I'm looking for a way to offset an arbitrary curve defined through xy-coordinates in one direction (in R). I can use the {polyclip} package to offset the curve in two directions.
library(polyclip)
#> polyclip 1.10-0 built from Clipper C++ version 6.4.0
# Make a curve
t <- seq(10, 0, by = -0.05)
curve <- data.frame(
x = t * cos(t), y = t * sin(t)
)
plot(curve, type = 'l')
# Find offset
offset <- polylineoffset(curve, delta = 0.5,
jointype = "round", endtype = "openbutt")[[1]]
offset <- as.data.frame(offset) # xy coordinates
lines(offset, col = "red")
Because the points on the curve are more closely spaced than the offset's delta
parameter, I can heuristically split the offset by finding out where the distance between a point and the next is the largest.
distance <- c(0, sqrt(diff(offset$x)^2 + sqrt(diff(offset$y)^2)))
max_dist <- which.max(distance)
plot(curve, type = 'l')
lines(offset[1:(max_dist - 1), ], col = 3)
lines(offset[max_dist:nrow(offset), ], col = 4)
Created on 2021-11-11 by the reprex package (v2.0.1)
However, I would like to be able to split the offset, or offset in just one direction, even if the points on the curve are further apart than the offset distance. Is there a way to do this in R? I'm not married to the {polyclip} package, a solution using another package is fine too.