{beziersimplify} is a package with methods for simplifying/flattening
cubic beziers to polylines.
Three methods:
bezier3_to_points_n()- generate points atnequally spaced points along the bezierbezier3_to_points_t()- generate points at locations given by parametert(where alltvalues are in the range [0, 1])bezier3_to_points_tol()- generate enough points such that the maximum error of the resulting polyline is less than the given tolerance.
You can install the latest development version from GitHub with:
# install.package('remotes')
remotes::install_github('coolbutuseless/beziersimplify')# Random cubic bezier
set.seed(2026)
x <- runif(4)
y <- runif(4)In all plots:
- the endpoints are marked with a black dot,
- the 2 middle control points are marked in red
- the bezier drawn with
grid.bezier()shown in blue.
grid.newpage()
grid.circle(x, y, r = unit(1, 'mm'),
gp = gpar(fill = c('black', 'hotpink', 'hotpink', 'black'), col = NA))
grid.bezier(x, y, gp = gpar(col = 'blue'))Creates n evenly distributed points along the
pc <- bezier3_to_points_n(x, y, 10)
pc
#> x y
#> 1 0.6986735 0.5553690
#> 2 0.6422822 0.4132025
#> 3 0.5724589 0.3346112
#> 4 0.4960858 0.3112193
#> 5 0.4200456 0.3346509
#> 6 0.3512206 0.3965303
#> 7 0.2964934 0.4884818
#> 8 0.2627465 0.6021295
#> 9 0.2568623 0.7290977
#> 10 0.2857233 0.8610107Random locations along the bezier
t <- c(0, sort(runif(8)), 1)
t
#> [1] 0.000000000 0.005933257 0.153835241 0.231125047 0.252501179 0.356809315
#> [7] 0.580806337 0.691865940 0.848532462 1.000000000
pc <- bezier3_to_points_t(x, y, t)
pc
#> x y
#> 1 0.6986735 0.5553690
#> 2 0.6961146 0.5460333
#> 3 0.6166476 0.3759173
#> 4 0.5664895 0.3308138
#> 5 0.5520063 0.3231172
#> 6 0.4797605 0.3125503
#> 7 0.3372984 0.4150873
#> 8 0.2867535 0.5126543
#> 9 0.2553431 0.6819699
#> 10 0.2857233 0.8610107Points are distributed so as to meet the error tolerance requirement using as few points as possible.
Large tolerance
pc <- bezier3_to_points_tol(x, y, tol = 0.1)
pc
#> x y
#> 1 0.6986735 0.5553690
#> 2 0.5294672 0.3814636
#> 3 0.4309180 0.3445682
#> 4 0.3635237 0.3815322
#> 5 0.3089539 0.5198708
#> 6 0.2857233 0.8610107Small tolerance
pc <- bezier3_to_points_tol(x, y, tol = 0.00001)
head(pc)
#> x y
#> 1 0.6986735 0.5553690
#> 2 0.6906975 0.5275679
#> 3 0.6824853 0.5023608
#> 4 0.6740727 0.4795709
#> 5 0.6740727 0.4795709
#> 6 0.6646878 0.4570810



