generate_random_polygon: Generate random polygon

Description Usage Arguments Value Author(s) Examples

View source: R/generate_random_polygon.R

Description

This function generates a random polygon. It uses the polar coordinate representation, where the angles are evenly spaced over the interval [0, 2pi] and the radiuses are sampled from a uniform distribution.

Usage

1
2
3
4
5
6
7
generate_random_polygon(
  center = c(0, 0),
  min.radius = 0.5,
  max.radius = 1,
  num.vertices = 5,
  seed = NULL
)

Arguments

center

The "center" of the polygon, which determines its position. Should either be a 1 x 2 data frame/matrix or a numeric vector with 2 elements.

min.radius, max.radius

The minimum and maximum bounds used in the uniform distribution that samples the radius values

num.vertices

The number of vertices that the random polygon should contain

seed

The seed used when sampling the radius value

Value

A data frame that contains one row per vertex in the polygon, and the columns x and y.

Author(s)

Mathias Isaksen mathiasleanderi@gmail.com

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
# A random polygon with center (5, 10) and 10 vertices. The distance from
# the center to the vertices is between 4 and 10
vertex.df = generate_random_polygon(
  center = c(5, 10),
  min.radius = 4,
  max.radius = 10,
  num.vertices = 10,
  seed = 123
)

library(ggplot2)
# The center of the polygon is shown in red
ggplot()+
  geom_polygon(data = vertex.df, aes(x = x, y = y))+
  geom_point(aes(x = 5, y = 10), color = "red")+
  coord_fixed()

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