A polygon is a set of points defining the boundary of a region. A polygon can contain one or more holes. They must be fully contained inside the outer boundary.
Arguments
- ...
Vector of points, numerics, or a list. See the Constructors sections
- id
An integer vector of the same length as
points
, dividing the points into separate polygons (only used ifpoints
is a point vector)- hole_id
An integer vector of the same length as
points
, dividing the points into boundary and separate holes (only used ifpoints
is a point vector).- x
An object convertible to a polygon vector or a polygon vector
Constructors
Providing a 2D points vector and no
id
andholes_id
will construct a single polygon with no holesProviding a 2D points vector and an
id
vector will create a vector of polygons with no holesProviding a 2D points vector and a
holes_id
vector will construct a single polygon with holesProviding a 2D points vector, an
id
vector, and aholes_id
vector will construct a vector of polygons with holesFor all above, instead of a 2D point vector the x and y coordinates can be supplied directly
Providing a list of 2D point vectors will construct a vector of polygons with no holes
Providing a list of list of 2D point vectors will construct a vector of polygons with holes
Further, polygons can also be constructed from 2D segments and triangles
using the as_polygon()
function.
See also
To get a complete overview of the boolean operations possible with polygons see the dedicated help page on the topic
Other polygons:
polygon_set()
Examples
points <- point(
c(1, 0, -1, -0.5, 0.5),
c(0, 1, 0, -1, -1)
)
# Construct a single polygon from a vector of points
poly <- polygon(points)
poly
#> <2D polyclid_polygons [1]>
#> [1] [Boundary: 5, Range: <<-1, -1>, <1, 1>>, Holes: 0]
plot(poly, col = "grey")
# Or directly from x and y coordinates
poly <- polygon(
c(1, 0, -1, -0.5, 0.5),
c(0, 1, 0, -1, -1)
)
# Use id to split points into multiple polygons
poly <- polygon(points[c(1, 2, 3, 4, 5, 1)], id = rep(1:2, each = 3))
poly
#> <2D polyclid_polygons [2]>
#> [1] [Boundary: 3, Range: <<-1, 0>, <1, 1>>, Holes: 0]
#> [2] [Boundary: 3, Range: <<-0.5, -1>, <1, 0>>, Holes: 0]
plot(poly, col = "grey")
# Use a list of lists to define polygons with holes
poly <- polygon(list(
list(
points,
point(c(0, 0.5, -0.5), c(0.5, -0.5, -0.5))
)
))
plot(poly, col = "grey")
# or use hole_id to similar effect (same polygon as above)
poly2 <- polygon(
c(points, point(c(0, 0.5, -0.5), c(0.5, -0.5, -0.5))),
hole_id = c(1, 1, 1, 1, 1, 2, 2, 2)
)
poly == poly2
#> [1] TRUE
# Equality of polygons doesn't care about where on the ring the vertices start
poly <- polygon(points)
poly2 <- polygon(points[c(2:5, 1)])
poly == poly2
#> [1] TRUE
# It cares about orientation though
poly == reverse_orientation(poly)
#> [1] FALSE
# This have implications for unqiue and duplicated
polys <- c(poly, poly2, reverse_orientation(poly))
unique(polys)
#> <2D polyclid_polygons [2]>
#> [1] [Boundary: 5, Range: <<-1, -1>, <1, 1>>, Holes: 0]
#> [2] [Boundary: 5, Range: <<-1, -1>, <1, 1>>, Holes: 0]
duplicated(polys)
#> [1] FALSE TRUE FALSE