deform

fun LineSegment.deform(attractor: Vector2, strength: Double): Segment2D(source)

Applies a deformation to the line segment by introducing intermediate control points influenced by an attractor vector and a specified strength.

Return

A new Segment2D object with added control points representing the deformed line segment.

Parameters

attractor

The vector that acts as the attractor point influencing the deformation.

strength

The strength of the deformation, determining how significantly the line segment bends toward the attractor.


fun Segment2D.deform(attractor: Vector2, strength: Double = 1.0, falloff: Double = 2.0, refDistance: Double = 1.0, endpointPinning: Double = 0.0, errorTolerance: Double = 0.5, maxDepth: Int = 6, samplesPerPiece: Int = 24): List<Segment2D>(source)

Approximates the shape curve would take if every point on it were pulled toward attractor by a distance-weighted force, with both endpoints pinned. Adaptively splits into more pieces wherever a single cubic Bezier can't fit the displacement field within errorTolerance.

Displacement field (defined over the GLOBAL parameter t of the input curve, so taper only varies along the true curve, not per split piece):

D(t) = B(t) + strength * taper(t) * fall(t) * (attractor - B(t)) taper(t) = (1 - endpointPinning) + endpointPinning * 4t(1-t) fall(t) = 1 / (1 + (dist(t) / refDistance)^falloff)

taper(t) blends between two extremes: endpointPinning = 0 -> taper(t) = 1 everywhere, so the endpoints are pulled exactly as strongly (relative to their own distance) as any interior point - the whole curve is "attracted". endpointPinning = 1 -> taper(t) = 4t(1-t), which is 0 at t=0,1, reproducing the original pinned-rod behavior (fixed endpoints). Values in between give partial resistance at the ends.

Each candidate piece is fit with its endpoints FIXED to the true displaced position at its t-range boundaries (not the original curve's position) so neighboring pieces always join up exactly, regardless of endpointPinning.

Parameters

curve

original cubic Bezier

attractor

the attracting point

strength

overall pull strength; ~0 = no bend, ~1 = strong bend

falloff

distance falloff exponent (0 = uniform, 2 = gravity-like)

refDistance

normalizes falloff so it doesn't blow up near the attractor

endpointPinning

0 = endpoints move like any other point (fully attracted), 1 = endpoints stay fixed in place (fully pinned), values in between blend smoothly. Default is fully attracted.

errorTolerance

max allowed RMS fit error (same units as the curve's coordinates) before a piece is split in two

maxDepth

recursion limit; caps output at 2^maxDepth pieces. Keep this modest (5-8) — it's a hard ceiling, not a target.

samplesPerPiece

interior samples used both for fitting and for measuring the resulting error


fun ShapeContour.deform(attractor: Vector2, strength: Double, falloff: Double, refDistance: Double, errorTolerance: Double = 0.5, maxDepth: Int = 6, samplesPerPiece: Int = 24): ShapeContour(source)

Deforms the current ShapeContour by applying an attraction force toward a given attractor point. The deformation is influenced by various parameters including strength, falloff, and reference distance, and adaptively refines the contour where necessary to meet an error tolerance.

Return

A new ShapeContour representing the deformed version of the original.

Parameters

attractor

The vector representing the point toward which the contour is attracted.

strength

The magnitude of the attraction force, where higher values result in greater deformation.

falloff

The exponent controlling how the attraction force decreases with distance from the attractor.

refDistance

A reference distance that normalizes the falloff effect, preventing excessive influence near the attractor.

errorTolerance

The maximum allowed root mean square (RMS) error for approximating the deformed contour. Default is 0.5.

maxDepth

The maximum recursion depth for refining the contour, limiting the number of pieces in the output. Default is 6.

samplesPerPiece

The number of interior sample points used for both fitting and measuring deformation errors. Default is 24.