Polygon2D is an R6::R6Class() object representing a two-dimensional
polygon. It inherits from Coord2D so all transformation methods
(e.g. $rotate(), $translate()) and arithmetic operators work directly
on the polygon.
Super class
Coord2D -> Polygon2D
Active bindings
is_convexLogical indicating whether the polygon is convex (
TRUE), concave (FALSE), or unknown (NA).convex_hullA Polygon2D object of the convex hull. If the polygon is already convex (
$convex == TRUE) it returns itself. For concave polygons the hull is computed on demand and cached until the next transformation.normalsA Coord2D object of unit edge normals
edgesA Segment2D object of the
npolygon edges (computed on demand and cached until the next transformation).
Methods
Polygon2D$new()
Usage
Polygon2D$new(xyw, convex = NA)Arguments
xywA matrix with three columns representing (homogeneous) coordinates. The first two columns are x/y coordinates and the last column is all ones. Column names should be
"x","y", and"w".convexNA(default) to auto-detect convexity fromxyw,TRUEto assert convex (skip detection), orFALSEto mark as concave.
Polygon2D$print()
Arguments
nNumber of vertices to print. If
NULLprint all....Passed to
format.default().
Polygon2D$transform()
Usage
Polygon2D$transform(mat = transform2d())Arguments
matA 3x3 matrix representing a post-multiplied affine transformation matrix. The last column must be equal to
c(0, 0, 1). If the last row isc(0, 0, 1)you may need to transpose it to convert it from a pre-multiplied affine transformation matrix to a post-multiplied one. If a 2x2 matrix (such as a 2x2 post-multiplied 2D rotation matrix) we'll quietly add a final column/row equal toc(0, 0, 1).
Examples
vertices <- as_coord2d(x = c(0, 0.5, 1, 0.5), y = c(0.5, 1, 0.5, 0))
p <- as_polygon2d(vertices)
print(p)
#> <Polygon2D[4 vertices, convex = TRUE]>
#> x y w
#> [1,] 0.0 0.5 1
#> [2,] 0.5 1.0 1
#> [3,] 1.0 0.5 1
#> [4,] 0.5 0.0 1
p$is_convex
#> [1] TRUE
p$normals
#> <Coord2D[4]>
#> x y w
#> [1,] 0.7071068 -0.7071068 1
#> [2,] -0.7071068 -0.7071068 1
#> [3,] -0.7071068 0.7071068 1
#> [4,] 0.7071068 0.7071068 1
h <- p$convex_hull
# Transformations are inherited from Coord2D
p$rotate(degrees(45))
