Skip to contents

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_convex

Logical indicating whether the polygon is convex (TRUE), concave (FALSE), or unknown (NA).

convex_hull

A 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.

normals

A Coord2D object of unit edge normals

edges

A Segment2D object of the n polygon edges (computed on demand and cached until the next transformation).

Methods

Inherited methods


Polygon2D$new()

Usage

Polygon2D$new(xyw, convex = NA)

Arguments

xyw

A 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".

convex

NA (default) to auto-detect convexity from xyw, TRUE to assert convex (skip detection), or FALSE to mark as concave.


Polygon2D$print()

Usage

Polygon2D$print(n = NULL, ...)

Arguments

n

Number of vertices to print. If NULL print all.

...

Passed to format.default().


Polygon2D$transform()

Usage

Polygon2D$transform(mat = transform2d())

Arguments

mat

A 3x3 matrix representing a post-multiplied affine transformation matrix. The last column must be equal to c(0, 0, 1). If the last row is c(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 to c(0, 0, 1).


Polygon2D$clone()

The objects of this class are cloneable with this method.

Usage

Polygon2D$clone(deep = FALSE)

Arguments

deep

Whether to make a deep clone.

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))