Description Usage Arguments Value Author(s) Examples
View source: R/rotate-polygon.R
Takes in one or more polygons and rotates each one a specified amount.
1 | rotate_polygon(vertex.df, rotation, center.of.rotation = "centroid")
|
vertex.df |
A data frame where each row corresponds to the vertex of a polygon.
It must contain the columns |
rotation |
The amount each polygon is to be rotated in radians. Either a single
number or a vector containing one number per polygon in |
center.of.rotation |
The center of rotation, i.e. the point which the polygons will be rotated around. Can be one of the following:
|
The returned data frame is equal to vertex.df
everywhere, except
the x
- and y
-columns that are changed due to the rotation.
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 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()
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.