The radius offset of a path is a path that is some fixed distance away on one side from the original path. We can consider this in two steps. First compute the offset of each segment. Second join these offsets. Depending on what particular path you want the second step could be to join or trim the segments. Trimming is much harder as it is possible that a whole segment's offset could be "trimmed" (think of an inside corner involving multiple segments) so we would need to consider more then just adjacent segments. Without trimming the path will always lie inside a trimmed path and the original curve (note that when stroking the non-trimmed path it is possible to have artifacts outside the trimmed path due to miter join of an acute angle).
An alternative definition could be based on the perimeter of the union of all circles of radius for each parameter value in the path. This has the benefit of never being self-intersecting, but can define complex paths (non-continuous). A certain kind of aggressive trimming can be equivalent to this definition.
Some preliminary code is here: https://gist.github.com/fryguybob/3969759.
Offsetting a Linear segment is simply a translation by a perpendicular vector of length .
While at the time of writing this we do not have arc segments, it is useful to consider how they offset. Since arcs have a fixed radius of curvature they are already a constant distance from some center point. The offset is just the arc with the same start angle, end angle, and center, but with radius .
Now consider an inside corner. We can think of this as an arc with radius . if then our offset is in the opposite direction as the original and the center of the two arcs is between the two arcs. This might seem like the wrong thing but if you pick some parameter then the original arc and the offset arc will be exactly distance apart at that .
This might be hard to follow without a picture, but if you consider that inside corner with a Linear segment leading in and leading out then the offset of the linear segments (given a big enough ) will cross each other. Now to get from the end of to the start of with an arc we start going in the direction "backward" along , curve in the same direction (around the clock) as the arc, then end going "backward" along . Note that this gives us a potentially undesirable cusp with maximal "sharpness", but it is the path that fits our definition. Trimming would remove this.
Unlike the happy world of arcs we do not have a constant radius of curvature with Bézier curves. Lets first consider the most basic thing we could do. For the discussion we will use a fixed segment (absolute points) curve and name the points , , , and . We can get part of the way to the offset by getting the ends right. Note that at the start and end the curve is tangent to the lines and respectively. If we make our offset curve by offsetting and then we will have an offset that is correct at the ends. We will call this offset the -offset.
This sets us up to do what we always do with Bézier curves, subdivide. First we produce the -offset, then we check some values and see if we are close. We can check each by finding the point at that parameter then looking at the distance to the -offset at that same value. If we are within some we use the -offset. Otherwise we split in half and recurse. A version of this approach is here: https://gist.github.com/fryguybob/4945944 (this has been updated to include scaling based on curvature).
A few things to note. If we look at the length of the -offset it is going to consistently undershoot when the radius of curvature is positive (counter-clockwise) and overshoot when negative (considering only original curves with curvatures with a constant sign). Other (single segment) offsets are only two degrees of freedom away from the -offset. Specifically the lengths of the offset and (from here on called the handle lengths) are the only wiggle room we have.
Considering this we can see that while our subdivided -offset will always give a curve within (ignoring a potential problem with the sampling) of the true offset, the first derivative will be rather noisy when compared to the offset. This is unfortunate as offsets could be quite useful for things like animations where it would be quite easy to perceive velocity oscillations.
One possible way to approximate how to set the handle lengths could be to use the fact that subdivision will eventually lead to monotonic segments (the radius of curvature has a constant sign, or the curve only bends one way), or a segment with an inflection point that is within . It would probably be good to split on inflection points (in a single step at the beginning) anyway to get to these monotonic segments faster and more precisely. Once we are in this form we can treat the segment like we did the arc by producing the -offset, then scaling the handle lengths by the ratio of the radius of curvature and that value plus . We will call this the -offset (this is now what is implemented in https://gist.github.com/fryguybob/4945944).
We can construct cases much like the example given with two linear segments and an arc in-between with a single Cubic segment. For the right value we would then get two cusps in the true offset. This alone shows that the offset cannot be described exactly with a single segment.
The order of the true offset should determine how many samples we need to test to know if the whole curve is within . This analysis remains to be done.
There is an opposite corner case where the segment starts with a cusp. If we first split the curve on the cusp we now need to join the offsets back together. Interestingly enough, if we don't split the cusp we get an approximation of this with just subdivision but it isn't clear how robust this is. It will probably be better to assume that we might have a join in the middle of a segment. This is unfortunate as it would be nice in some cases to be able to produce both the offset and the original with the same subdivisions as this gives a nice correlation of -values between the two curves. In this situation we could give the original with an added zero length segment to match the join.
It appears that we should be able to do all of this over the rational numbers. All we need are some functions for the first and second derivative (curvature is available in the branch: https://github.com/diagrams/diagrams-lib/pull/74).
When we have a corner between line segments there is no longer a single point that is distance from that curve and is a closest point but an arc of such points. If this was a marching band taking a corner this is like the outer marchers having to march quickly while the inner marcher comes to a stop. All that is needed to construct the join segment is the point of the corner from the original path and the end point of the first offset segment and the start point of the second.
Joins also give an opportunity for different geometry based on those three points. For example any common line join styles such as, miter, round (this is the -offset curve), and bevel. There are other styles useful in pattern making such as mirrored and notched joins. Note that we can recover the tangent vectors as perpendiculars to the lines from the original to one of the ends so we do not need that additional information.
I don't have much to say here. There could be other uses for trimming as it is finding the "nearest" intersection and throwing away part of the curve.
It is interesting to note that in three dimensions we can reuse our alternative definition but with a union of spheres instead of a circle.
It would be very useful to have a collection of curve fitting techniques.
Sometimes is is easy to generate a large number of segments, but in the end we would like a simpler curve that may be difficult to obtain constructively in the first place. One approach to this is to use curve fitting to regenerate the curve.