CardinalSpline
Cardinal spline interpolation for smooth curves through control points.
This class provides utilities for creating smooth curves that pass through a series of control points using Cardinal spline interpolation. Cardinal splines are a type of cubic Hermite spline where tangents are calculated automatically from neighboring points.
Features
- Smooth Interpolation: Creates C1 continuous curves through all points
- Adjustable Tension: Control curve tightness (0 = sharp, 1 = loose)
- Variable Resolution: Specify segments between points for quality
- Closed Curves: Option to create continuous loops
- Performance Optimized: Pre-cached calculations for efficiency
Usage Example
// Define control points [x1,y1, x2,y2, ...]
var points = [100,100, 200,150, 300,100, 400,200];
// Generate smooth curve points
var smooth = CardinalSpline.getCurvePoints(
points,
0.5, // tension
20, // segments per curve
false // not closed
);
// Draw the smooth curve
var line = new Line();
line.points = smooth;
Algorithm
Cardinal splines use the positions of four consecutive points (P0, P1, P2, P3) to calculate the curve between P1 and P2. The tangent at each point is determined by the vector between its neighbors, scaled by the tension parameter.
Static Members
getCurvePoints(points: Array<Float>, ?tension: Float = 0.5, ?numSegments: Int = 25, ?close: Bool = false, ?result: Array<Float>): Null<Array<Float>>Calculates an array containing points representing a cardinal spline through given point array. Points must be arranged as: [x1, y1, x2, y2, ..., xn, yn].
The points for the cardinal spline are returned as a new array.
| Name | Type | Default | Description |
|---|---|---|---|
points |
Array<Float> | Point array containing at least 2 points (4 values). Format: [x1, y1, x2, y2, ..., xn, yn] | |
tension |
Float | 0.5 |
Curve tension parameter. Default: 0.5 - 0.0 = sharp corners (Catmull-Rom spline) - 0.5 = balanced curve - 1.0 = loose curve - Can exceed [0,1] for special effects |
numSegments |
Int | 25 |
Number of interpolated points between each pair of control points. Higher values create smoother curves. Default: 25 |
close |
Bool | false |
Whether to connect the last point back to the first, creating a closed loop. Default: false |
result |
Array<Float> | (optional) | Optional array to store results in (avoids allocation). Will be resized as needed. |
| Returns | Description |
|---|---|
| Null<Array<Float>> | Array containing interpolated points [x1,y1, x2,y2, ...] Length = (numPoints-1) * numSegments * 2 + 2 (+ extra for closed) * haxe // Create a smooth curve through 4 points var controlPoints = [ 100, 100, // Point 1 200, 50, // Point 2 300, 150, // Point 3 400, 100 // Point 4 ]; * var smoothCurve = CardinalSpline.getCurvePoints( controlPoints, 0.5, // Medium tension 30 // 30 segments between points ); |
Private Members
Cached hermite basis function values for performance. Reused across multiple spline calculations.
parse(pts: Array<Float>, cache: Array<Float>, l: Int, tension: Float, numSegments: Int, result: Array<Float>, rPos: Int): IntInternal function that performs the actual spline interpolation. Uses cached hermite basis functions for performance.
| Name | Type | Description |
|---|---|---|
pts |
Array<Float> | Extended point array with duplicated endpoints |
cache |
Array<Float> | Pre-calculated hermite basis function values |
l |
Int | Original point array length |
tension |
Float | Spline tension parameter |
numSegments |
Int | Segments between control points |
result |
Array<Float> | Output array for interpolated points |
rPos |
Int | Current position in result array |
| Returns | Description |
|---|---|
| Int | Updated position in result array |