rotate_polygon: Rotate polygons

Description Usage Arguments Value Author(s) Examples

View source: R/rotate-polygon.R

Description

Takes in one or more polygons and rotates each one a specified amount.

Usage

1
rotate_polygon(vertex.df, rotation, center.of.rotation = "centroid")

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.

rotation

The amount each polygon is to be rotated in radians. Either a single number or a vector containing one number per polygon in vertex.df.

center.of.rotation

The center of rotation, i.e. the point which the polygons will be rotated around. Can be one of the following:

  • "centroid" (default), where each polygon is rotated around its centroid

  • A 1 x 2 matrix data frame/matrix that specifices the x- and y-coordinate of the center of rotation used for every polygon (a numeric vector of length 2 is also accepted, i.e. c(x, y))

  • A n x 2 data frame/matrix, where row number i contains the x- and y-coordinates of the center of rotation for polygon number i in vertex.df, and n is the number of polygons.

Value

The returned data frame is equal to vertex.df everywhere, except the x- and y-columns that are changed due to the rotation.

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
30
31
32
polygon.center = data.frame(x = c(0, 2, 4), y = c(0, 2, 4))
original.df = compute_regular_polygons(
  center = polygon.center,
  radius = c(1, 1, 1.5),
  num.edges = 3:5
)

# Rotating every polygon the same amount, using the centroids as centers of rotation
centroid.df = rotate_polygon(original.df, rotation = pi/8, center.of.rotation = "centroid")
# Rotating each polygon a different amount, around a different center of rotation
center.of.rotation = data.frame(x = c(2, 0, 4), y = c(0, 2, 0))
manual.df = rotate_polygon(
  original.df,
  rotation = c(pi/4, pi, -pi/2),
  center.of.rotation = center.of.rotation
)

# The plot below shows both the original and rotated polygons.
# The black lines connect the center of rotation used in manual.df to the centroids
# of the original polygons.
library(ggplot2)
ggplot()+
  geom_polygon(data = original.df, aes(x = x, y = y, group = group, color = "1. Original"),
               fill = NA)+
  geom_polygon(data = centroid.df, aes(x = x, y = y, group = group, color = "2. Centroid"),
               fill = NA)+
  geom_polygon(data = manual.df, aes(x = x, y = y, group = group, color = "3. Manual"),
               fill = NA)+
  geom_segment(aes(x = polygon.center$x, xend = center.of.rotation$x,
                   y = polygon.center$y, yend = center.of.rotation$y))+
  scale_color_manual("", values = c("red", "green", "blue"))+
  coord_fixed()

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