Description Usage Arguments Details Value Note Author(s) Examples
This function takes in an arbitrary number of polygons and cuts them a given number of times. For each iteration, every polygon is cut by a randomly generated line, which divides it into several parts.
1 2 3 4 5 6 7 | cut_polygons(
vertex.df,
number.of.iterations,
use.centroid = TRUE,
seed = NULL,
return.all = FALSE
)
|
vertex.df |
A data frame where each row corresponds to the vertex of a polygon.
It must contain the columns |
number.of.iterations |
The number of times that the polygons are cut in
half. Either a single value, or a numeric vector with one value per polygon in |
use.centroid |
If |
seed |
The seed used when generating the directions of the cut lines. When no value is specified, the seed is not set. |
return.all |
If |
The directions of the lines are sampled from a uniform distribution.
A data frame where each row corresponds to the vertex of a polygon, with the following columns:
x, y |
The coordinates of the vertex |
group |
The index of the polygon that the vertex belongs to |
iteration |
The iteration that the polygon belongs to (only present when |
number.of.iterations
should be chosen with care, as each
iteration at least doubles the number of polygons, leading to exponential growth.
Mathias Isaksen mathiasleanderi@gmail.com
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | # Example with single initial polygon (square)
vertex.df = data.frame(
x = c(-1, 1, 1, -1),
y = c(1, 1, -1, -1)
)
cut.df = cut_polygons(vertex.df, number.of.iterations = 10, seed = 123)
library(ggplot2)
ggplot()+
geom_polygon(data = cut.df,
aes(x = x, y = y, group = group),
fill = NA, color = "black")+
coord_fixed()
# Example with multiple initial polygons (square and triangle)
vertex.df = data.frame(
x = c(-1, 1, 1, -1, 1, -1, 0),
y = c(1, 1, -1, -1, 2, 2, 2+sqrt(3)),
group = c(rep(1, 4), rep(2, 3))
)
# Cut square and triangle 10 and 9 times, respectively
cut.df = cut_polygons(vertex.df, number.of.iterations = c(10, 9), seed = 123)
ggplot()+
geom_polygon(data = cut.df,
aes(x = x, y = y, group = group),
fill = NA, color = "black")+
coord_fixed()
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.