filletCorners

fun List<Vector2>.filletCorners(radius: Double, closed: Boolean, reverseCurves: Boolean = true, polarity: YPolarity = YPolarity.CW_NEGATIVE_Y): ShapeContour(source)

Converts this polyline into a contour in which the corners are replaced by circular arcs of radius that keep the tangent direction continuous along the whole contour.

Every corner gets an arc that is tangent to both adjacent edges (a fillet). Where two consecutive corners turn in opposite directions and their fillets don't fit on the edge between them, and reverseCurves is true, the pair is replaced by a reverse curve (S-bend): two arcs of radius that turn in opposite directions and meet tangentially at an inflection point on the middle edge. The incoming and outgoing edges are kept, the middle edge is replaced. At the point where the fillets just fit, the S-bend coincides with them.

Arcs that still don't fit next to their neighbours are shrunk: the length of every edge is shared between the arcs at its two ends in proportion to what they need. Fillets of same-direction corners on short edges, and all fillets when reverseCurves is false, therefore get a smaller radius. For open polylines the first and last edge can be consumed entirely by their single arc. Collinear corners are left as they are and corners that reverse direction (a 180 degree turn) can't be filleted and stay sharp.

Arcs are approximated with one cubic Bézier per 90 degrees of turn, the radial error is below 0.03% of the radius.

Parameters

radius

the arc radius

closed

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

reverseCurves

whether pairs of opposite corners on a short edge are joined by an S-bend of two opposite arcs instead of two shrunk fillets

polarity

the polarity of the resulting contour