Skip to contents

aabb_polygon2d() creates an axis-aligned bounding box Polygon2D object covering the range of x.

Usage

aabb_polygon2d(x, ...)

Arguments

x

A 2D object with a range() method that returns a Coord2D of length two (e.g. Coord2D, Ellipse2D, Segment2D).

...

Passed to range().

Value

A Polygon2D object with $is_convex == TRUE whose vertices are in counter-clockwise order.

See also

rectangle_polygon2d() for creating a rectangle by center and dimensions. range() for the bounding-range methods.

Examples

# Bounding box for a set of points
pts <- as_coord2d(
  x = runif(20, min = 1, max = 3),
  y = runif(20, min = 1, max = 5)
)
p <- aabb_polygon2d(pts)
p$is_convex
#> [1] TRUE
plot(p, border = "black", col = "cyan")
points(pts, pch = 16, cex = 1.5)


# Bounding box for an ellipse
e <- as_ellipse2d(as_coord2d(1, 1), rx = 2, ry = 1, theta = degrees(30))
p2 <- aabb_polygon2d(e)
plot(p2, border = "black", col = "cyan")
lines(e, col = "black", lwd = 2)