splines/bezier
Beziér splines
Beziér splines are useful for drawing strokes and shapes and are often the dominant parametric curve exposed by graphical editors.
Types
A generic n-ary Beziér spline over the domain a. Most users will want the simpler versions produced by new_2d or new_3d.
If you construct the spline yourself, you must supply an Interpolator
function for the domain. The number of points in the domain
determines both the order and the computational-complexity of
sampling the domain using the de Castlejau algorithm.
| Number of points | Order | Complexity |
|---|---|---|
| 2 | Linear | 1 step |
| 3 | Quadratic | 3 steps |
| 4* | Cubic | 6 steps |
| 5 | Quartic | 10 steps |
| … | … | … |
If you use new_2d or new_3d, and you pass 4 points, this will create instead the CubicBezier variant automatically.
Sampling of this variant will use the matrix-multiplication formulation instead of de Casteljau’s method.
pub type Bezier(a) {
Bezier(points: List(a), interpolator: fn(a, a, Float) -> a)
CubicBezier(
points: vec4.Vec4(a),
scale: fn(a, Float) -> a,
sum: fn(List(a)) -> a,
)
}
Constructors
-
Bezier(points: List(a), interpolator: fn(a, a, Float) -> a) -
CubicBezier( points: vec4.Vec4(a), scale: fn(a, Float) -> a, sum: fn(List(a)) -> a, )Special case of the most common kind of Bezier curve
Values
pub fn new_2d(
points: List(vec2.Vec2(Float)),
) -> Bezier(vec2.Vec2(Float))
Constructs an n-ary Bezier spline in two dimensions. This spline should include at least two points, but that constraint is not checked.
pub fn new_3d(
points: List(vec3.Vec3(Float)),
) -> Bezier(vec3.Vec3(Float))
Constructs an n-ary Beziér spline in three dimensions. This spline should include at least two points, but that constraint is not checked.
pub fn sample(b: Bezier(a), t: Float) -> a
Samples the Beziér spline at time t using the recursive de Castlejau algorithm.
Panics if the curve contains no points.