cut_polygons: Repeated cutting of polygons

Description Usage Arguments Details Value Note Author(s) Examples

View source: R/cut-polygons.R

Description

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.

Usage

1
2
3
4
5
6
7
cut_polygons(
  vertex.df,
  number.of.iterations,
  use.centroid = TRUE,
  seed = NULL,
  return.all = FALSE
)

Arguments

vertex.df

A data frame where each row corresponds to the vertex of a polygon. It must contain the columns x, y, and optionally group (must be integer). x and y specify the coordinates of the vertex, and group is used to indicate which polygon the vertex belongs to. If vertex.df contains only a single polygon, group can be omitted.

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 vertex.df.

use.centroid

If TRUE, the randomly generated line goes through the centroid of the polygon. Otherwise, the line goes through a point sampled uniformly from the interior of the polygon.

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 TRUE, the function will return a data frame that contains every iteration (i.e. the state after iteration 1,...,number.of.iterations).

Details

The directions of the lines are sampled from a uniform distribution.

Value

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 return.all = TRUE)

Note

number.of.iterations should be chosen with care, as each iteration at least doubles the number of polygons, leading to exponential growth.

Author(s)

Mathias Isaksen mathiasleanderi@gmail.com

Examples

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

mathiasisaksen/artKIT documentation built on Dec. 21, 2021, 2:52 p.m.