Description Usage Arguments Value Note Author(s) Examples
View source: R/transition-between-polygons.R
Function that interpolates/transitions between two polygons. This is done by
computing (1 - time)*start.polygon + time*end.polygon
.
1 2 3 4 5 6 | transition_between_polygons(
start.polygon,
end.polygon,
time,
vertex.order = "reorder"
)
|
start.polygon, end.polygon |
Dataframes/matrices containing the vertices of the polygons to interpolate between. The coordinates of the vertices must be stored in columns named "x" and "y", and the polygons must contain the same number of vertices. |
time |
The times at which we are interested in interpolating the polygons. |
vertex.order |
Determines whether and how the vertices in |
A data frame that contains one row per vertex. If start.polygon
and end.polygon
contain n vertices, and time
contains m values, then
the returned data frame will contain n*m rows. following columns:
x, y |
The coordinates of the vertex |
group |
Which polygon the vertex belongs to (1 for the first value in |
time |
The time value of the associated polygon |
It is recommended to ensure that the start and end polygons have the correct
orientation and numbering of vertices before computing the transition, and then using
vertex.order = "preserve"
.
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 33 34 35 36 37 38 39 40 41 42 43 | # Example: Transition from hexagon to square
# Create hexagon
hexagon.df = compute_regular_polygons(
center = c(0, 0),
radius = 1,
rotation = 0,
num.edges = 6
)
# Round corners slightly
hexagon.df = round_polygon_corners(hexagon.df, corner.radius.scale = 0.3)
# Create square
square.df = compute_regular_polygons(
center = c(20, -20),
radius = 2,
rotation = 0,
num.edges = 4
)
# Round corners slightly
square.df = round_polygon_corners(square.df, corner.radius.scale = 0.3)
# Resample polygons with many vertices, so that the transition becomes smooth
num.vertices = 1000
resample.time = seq(0, 1, length.out = num.vertices + 1)[-(num.vertices + 1)]
hexagon.resample = interpolate_polygon(hexagon.df)(resample.time)
square.resample = interpolate_polygon(square.df)(resample.time)
# Show transition over 10 steps
num.transition = 10
transition.time = seq(0, 1, length.out = num.transition)
# Use vertex.order = "preserve" (both polygons are CCW, and have the top vertex
# as the first in hexagon.df and square.df)
transition.df = transition_between_polygons(
hexagon.resample,
square.resample,
transition.time,
"preserve")
# Show the result:
library(ggplot2)
ggplot()+
geom_polygon(data = transition.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.