round_polygon_corners: Rounds the corners of a polygon

Description Usage Arguments Value Note Author(s) Examples

View source: R/round-polygon-corners.R

Description

Takes in a single polygon and rounds it by replacing the corner vertices with circular arcs.

Usage

1
2
3
4
5
6
round_polygon_corners(
  vertex.df,
  corner.radius = "constant",
  corner.radius.scale = 1,
  max.vertices.per.corner = 50
)

Arguments

vertex.df

A data frame where each row corresponds to a vertex in the polygon. It must contain the columns x and y where x and y specify the coordinates of the vertex,

corner.radius

Determines the corner radius of each circular art. Can be one of the following:

  • A single value, which will be used for every circular arc/corner

  • A numeric vector of length nrow(vertex.df), where each value specifies the corner radius used for the corresponding vertex

  • "constant", where every corner uses the same, optimal radius. This is the largest possible radius while keeping things "nice and smooth"

  • "varying", which computes the largest possible corner radius for each vertex

corner.radius.scale

A number that each corner radius is multiplied by. Useful when corner.radius is "constant" or "varying", and you wish to reduce the size of the corner radius values.

max.vertices.per.corner

Controls the number of vertices used in the circular arcs that replace the corners. This depends on how sharp the corner is: the sharper the corner, the more vertices are needed to create a smooth arc.

Value

A data frame that contains the x- and y-coordinates of the rounded polygon.

Note

If the corner radius values are specified manually, the results will not necessarily look good, and it may take some trial and error. The option "varying" is experimental, and can be bit aggressive. See the example for a comparison between the different options.

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
# Generate a random polygon based on polar coordinates
set.seed(321)
n = 20 # Number of vertices
theta = rev(seq(0, 2*pi, length.out=n)[-(n + 1)])
radius = runif(n, 10, 20)
vertex.df = data.frame(x = radius*cos(theta), y = radius*sin(theta))
#'
# Plot original polygon
library(ggplot2)
ggplot() + geom_polygon(data = vertex.df, aes(x = x, y = y))+coord_fixed()
#'
# Every corner is rounded with the same specified radius
rounded.1 = round_polygon_corners(vertex.df, corner.radius = 1)
ggplot() + geom_polygon(data = rounded.1, aes(x = x, y = y)) + coord_fixed()
#'
# The corners are rounded with individual, specified radius
rounded.2 = round_polygon_corners(vertex.df, corner.radius = runif(n, 0, 1.9))
ggplot() + geom_polygon(data = rounded.2, aes(x = x, y = y)) + coord_fixed()
#'
# Every corner is rounded with the same optimal radius
rounded.3 = round_polygon_corners(vertex.df, corner.radius = "constant")
ggplot() + geom_polygon(data = rounded.3, aes(x = x, y = y)) + coord_fixed()
#'
# The corners are rounded with optimal individual radius
rounded.4 = round_polygon_corners(vertex.df, corner.radius = "varying")
ggplot() + geom_polygon(data = rounded.4, aes(x = x, y = y)) + coord_fixed()

# Comparison of different options
ggplot()+
  geom_polygon(data = rounded.1, aes(x = x, y = y, fill = "1. Constant, manual"))+
  geom_polygon(data = rounded.2, aes(x = x + 40, y = y, fill = "2. Varying, manual"))+
  geom_polygon(data = rounded.3, aes(x = x, y = y - 40, fill = "3. Constant, optimal"))+
  geom_polygon(data = rounded.4, aes(x = x + 40, y = y - 40, fill = "4. Varying, optimal"))+
  coord_fixed()

# Below we what happens if the corner radius is too large:
# The circular arcs are not connected in a smooth way
rounded.5 = round_polygon_corners(vertex.df, corner.radius = 4)
ggplot() + geom_polygon(data = rounded.5, aes(x = x, y = y)) + coord_fixed()

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