Description Usage Arguments Value Note Author(s) Examples
View source: R/round-polygon-corners.R
Takes in a single polygon and rounds it by replacing the corner vertices with circular arcs.
1 2 3 4 5 6 | round_polygon_corners(
vertex.df,
corner.radius = "constant",
corner.radius.scale = 1,
max.vertices.per.corner = 50
)
|
vertex.df |
A data frame where each row corresponds to a vertex in the polygon.
It must contain the columns |
corner.radius |
Determines the corner radius of each circular art. Can be one of the following:
|
corner.radius.scale |
A number that each corner radius is multiplied by.
Useful when |
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. |
A data frame that contains the x- and y-coordinates of the rounded polygon.
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.
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 | # 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()
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.