Skip to contents

shape_polygon is a drawable defined by a centroid and a radius; its vertices are n evenly spaced points around the circumference, giving a regular n-gon (e.g. n = 3 a triangle, n = 6 a hexagon).

shape_polygons() is a vectorized version of shape_polygon(): each argument may be a vector, recycled against the others. The result is a sketch containing one shape_polygon() per recycled row, rather than a single drawable.

Usage

shape_polygon(x = 0, y = 0, radius = 1, n = 6L, trans = trans_identity(), ...)

shape_polygons(x = 0, y = 0, radius = 1, n = 6L, trans = trans_identity(), ...)

Arguments

x, y

Centroid coordinates. Default 0.

radius

Radius. Must be non-negative. Default 1.

n

Number of sides (vertices). Must be at least 3. Default 6L.

trans

A trans object applied to the shape's computed points. Default trans_identity() (no transform).

...

Arguments passed to style().

Value

A drawable.

For shape_polygons(), a sketch.

Details

Unlike shape_circle()/shape_blob(), points does not repeat its first vertex at the end – there are exactly n distinct vertices, since grid::polygonGrob() closes the path itself and a polygon has no approximation error to hide.

Recycling uses purrr::pmap()'s own vctrs-based rules: any length-1 element is broadcast to the common length; mismatched lengths greater than 1 raise an error.

Examples

draw(shape_polygon(radius = 1, n = 3))

draw(shape_polygon(x = 1, y = 1, radius = 0.5, n = 8, color = "darkred"))

draw(shape_polygon(n = 5, fill = fill_stripe(color = c("white", "grey30"))))


# rotating a low-n polygon reorients its vertices
draw(shape_polygon(n = 4, trans = trans_rotate(pi / 4)))


draw(shape_polygons(x = 1:3, n = c(3L, 4L, 6L)))

draw(shape_polygons(
  x = 0, y = 0, radius = seq(3, 0.5, length.out = 6), n = 6,
  fill = rep(c("grey20", "grey90"), length.out = 6)
))