transition_between_polygons: Transition between polygons

Description Usage Arguments Value Note Author(s) Examples

View source: R/transition-between-polygons.R

Description

Function that interpolates/transitions between two polygons. This is done by computing (1 - time)*start.polygon + time*end.polygon.

Usage

1
2
3
4
5
6
transition_between_polygons(
  start.polygon,
  end.polygon,
  time,
  vertex.order = "reorder"
)

Arguments

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. time = 0 gives start.polygon, while time = 1 gives end.polygon. Can be either a single number or a numeric vector. If time contains values outside [0, 1], a warning will be given.

vertex.order

Determines whether and how the vertices in start.polygon and end.polygon are reordered before the interpolation is computed. If vertex.order = "preserve", no reordering is applied, and the polygons are used as-is. If vertex.order = "reorder", the function first ensures that the polygons have the same orientation (i.e. clockwise/counter-clockwise). Then, it attempts to shift the indices of the vertices so that the corresponding vertices on start.polygon and end.polygon are "aligned".

Value

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, 2 for the second and so on)

time

The time value of the associated polygon

Note

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".

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

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