clothoids

fun List<Vector2>.clothoids(closed: Boolean, tangents: List<Vector2>? = null, polarity: YPolarity = YPolarity.CW_NEGATIVE_Y): ShapeContour(source)

Converts this polyline into a clothoid spline: a contour of clothoid (Euler spiral) segments, along which the curvature changes linearly with arc length, that passes through all points.

When tangents is null the tangent direction at every point is solved for such that the curvature is continuous where segments meet, which makes the contour G2: the tangent direction and the curvature never jump. The ends of an open polyline get zero curvature, like the free ends of a bent elastic strip. The system is solved by Newton's method, starting from Catmull-Rom tangents. Points on a circle give that circle and collinear points a straight line.

When tangents are given every segment is the clothoid between its end points with those directions, which makes the contour G1: curvature can jump at the points.

Every segment is fitted after Bertolazzi and Frego, "G1 fitting with clothoids" (2015), and approximated with cubic Béziers that turn by at most π/16 each.

Some erratic point sets have no curvature-continuous solution that the solve can reach, such as some closed figure-eights of four points; about 2 in 100 random point sets in the tests. The tangents with the smallest curvature jumps found are then used, so the contour is still G1.

Parameters

closed

whether the polyline is closed, the last point should then not repeat the first

tangents

the tangent direction at every point, or null to solve for curvature continuity

polarity

the polarity of the resulting contour