shape_rectangle is a drawable defined by a centroid and a width/height,
rendered as an axis-aligned rectangle.
shape_rectangles()/shape_squares() are vectorized versions of
shape_rectangle()/shape_square(): each argument may be a vector,
recycled against the others. The result is a sketch containing one
shape_rectangle()/shape_square() per recycled row, rather than a
single drawable.
Usage
shape_rectangle(
x = 0,
y = 0,
width = 1,
height = 1,
trans = trans_identity(),
...
)
shape_square(x = 0, y = 0, side = 1, trans = trans_identity(), ...)
shape_rectangles(
x = 0,
y = 0,
width = 1,
height = 1,
trans = trans_identity(),
...
)
shape_squares(x = 0, y = 0, side = 1, trans = trans_identity(), ...)Arguments
- x, y
Centroid coordinates. Default
0.- width, height
Rectangle dimensions. Must be non-negative. Default
1.- trans
A trans object applied to the shape's computed points. Default
trans_identity()(no transform).- ...
Arguments passed to
style().- side
Side length, for
shape_square(). Must be non-negative. Default1.
Details
shape_square() is a thin convenience wrapper around shape_rectangle()
for the common case of width == height, taking a single side argument
instead – it returns a shape_rectangle object directly rather than
being its own class.
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_rectangle(width = 2, height = 1))
draw(shape_square(x = 1, y = 1, side = 0.5, color = "darkred"))
draw(shape_rectangle(width = 2, height = 1, fill = fill_checker()))
# trans_rotate() tilts a rectangle about its own centroid by default
draw(shape_rectangle(width = 2, height = 0.6, trans = trans_rotate(pi / 8)))
draw(shape_rectangles(x = 1:3, width = c(0.5, 1, 1.5), height = 0.5))
draw(shape_squares(x = 1:3, side = c(0.5, 1, 1.5)))
# a checkerboard of squares, alternating fill colour
grid_xy <- expand.grid(x = 1:5, y = 1:3)
draw(shape_squares(
x = grid_xy$x, y = grid_xy$y, side = 0.9,
fill = c("grey20", "grey90")[(grid_xy$x + grid_xy$y) %% 2 + 1]
))