-
Notifications
You must be signed in to change notification settings - Fork 18
pymath.interpolate_cubic
Daniel Flassig edited this page Jul 14, 2026
·
3 revisions
Interpolates a smooth cubic curve (B-Spline) through a list of points, returned as cubic Bézier segments.
pymath.interpolate_cubic(type, points [, start_tangent, end_tangent])| Parameter | Type | Description |
|---|---|---|
type |
string |
"open" or "closed" (as in pytha.create_polyline). |
points |
list of points | Array of points to interpolate. Each point can be either an array of length d for d-dimensional space or a number in 1D. |
start_tangent |
point (optional) | For an "open" curve only, the tangent direction at the first point — the same shape as a point |
end_tangent |
point (optional) | For an "open" curve only, the tangent direction at the last point. |
| Type | Description |
|---|---|
curve |
A flat list of control points describing the cubic Bézier segments, or nil if the curve could not be built (see Notes). Each control point has the same shape as an input point (a d-vector, or a plain number when points was given as a flat list of numbers). Successive segments share their joint control point: points 1..4 are the first segment, 4..7 the second, and so on. An "open" curve returns 3 * (n - 1) + 1 control points, a "closed" curve 3 * n (its last segment closes back to the first point). |
- The curve passes through every input point: control point
3 * k + 1equals input pointk + 1. - Each group of four consecutive control points is a cubic Bézier segment that can be passed straight to
pymath.eval_bezier. - For
"closed", give the distinct vertices — do not repeat the first point at the end. -
start_tangent/end_tangentare only allowed for an"open"curve; passing one with"closed"raises an error. Without them the curve leaves its endpoints with a zero derivative. Only the direction is used — the tangent's length is ignored. -
nilis returned when two consecutive points coincide.
local pts = {{0, 0}, {1, 2}, {3, 1}, {4, 3}}
local cp = pymath.interpolate_cubic("open", pts)
-- #cp == 10 -- 3 * (4 - 1) + 1 control points
-- cp[1] == {0, 0}, cp[4] == {1, 2}, cp[7] == {3, 1}, cp[10] == {4, 3}
-- sample the first Bézier segment at its midpoint
local mid = pymath.eval_bezier(0.5, cp[1], cp[2], cp[3], cp[4])
-- prescribe the end tangent directions (open curve): leave horizontally, arrive vertically
local cp2 = pymath.interpolate_cubic("open", pts, {1, 0}, {0, 1})
-- 1-D points as a flat list of numbers -> a flat list of numbers back
local y = pymath.interpolate_cubic("open", {0, 1, 3, 4})
-- #y == 10, y[1] == 0, y[4] == 1, y[7] == 3, y[10] == 4 (all plain numbers)Minimum PYTHA Version: V27