constexpr
constexpr

Reputation: 1041

Sort cubic bezier splines around a point by outgoing tangent

I need to sort cubic bezier splines around a point by their outgoing tangent. My first attempt was to determine the outgoing tangent angle and sort by that. For a spline with four points p0, p1, p2, p3, the outgoing tangent angle is:

p0 != p1 ? angle(p0, p1) : p0 != p2 ? angle(p0, p2) : angle(p0, p3)

This handles degenerate a cubic that is actually a quadratic or even a line. However, the point may have two outgoing splines with the same tangent angle but a different control point or end point position later on down the spline that impacts the sorting order.

Is there a nice closed-form algorithm for sorting two arbitrary bezier splines by outgoing angle that handles degenerate cases and also uses the rest of the spline to disambiguate cases when the tangents are equal? I can do it by trying t values until I disambiguate but it seems like there could be a closed-form algorithm.

Upvotes: 0

Views: 203

Answers (1)

hkrish
hkrish

Reputation: 1504

As far as I am aware of, there is no closed form solution for this particular problem. Consider that you wish to sort a set of curves by the outgoing tangent at a point; but if the tangents are equal at that point, the curves are already sorted! I understand that what you actually want is to find out whether the tangents diverge as you move along the curve away from the starting point.

So let's do that; compare the tangents at a small increment from t = 0 (t is the curve-time parameter of any parametric curve).

Here is a small demo in javascript.

The compareTan function compares the angle of the tangent at a small increment (t = 1e-5). All it depends on is a method to return the tangent at a given t parameter. It is not very hard to implement the quadratic case —for lines, you can compare them directly.

Upvotes: 0

Related Questions