svg_path/bezier
Lower-level helpers for Bezier curves.
Most users should work with svg_path.Line, svg_path.QuadraticBezier,
and svg_path.CubicBezier values through the root module. This module is
the more technical layer for users who want the curve math behind line and
Bezier segments.
A line segment is a degree-1 Bezier curve, a quadratic Bezier has one
control point, and a cubic Bezier has two control points. All evaluation
and splitting helpers use the standard Bezier parameter t:
bezier_point(curve, at: 0.0)is the curve start point.bezier_point(curve, at: 1.0)is the curve end point.bezier_derivative(curve, at: t)is the derivative with respect tot.split_bezier(curve, at: t)preserves the curve degree and divides it with de Casteljau’s algorithm.map_points(curve, with: f)maps the curve’s defining points.
The at value is not clamped. Values outside 0.0..1.0 extrapolate along
the same polynomial curve. split_bezier follows the same unclamped policy;
use split_bezier_inside when outside values should return an error.
split_bezier_many and split_bezier_inside_many sort their split points,
remove exact duplicates, and trim boundary 0.0 or 1.0 split points that
would only create zero-length boundary curves.
map_points maps the control points that define the curve. For nonlinear
functions, this is not the exact image of every point on the rendered curve;
it is the Bezier curve obtained by applying the function to the defining
points.
Types
Bezier-parameter representation of line, quadratic, and cubic curves.
pub type BezierData {
LinearBezierData(start: Point, end: Point)
QuadraticBezierData(start: Point, control: Point, end: Point)
CubicBezierData(
start: Point,
control1: Point,
control2: Point,
end: Point,
)
}
Constructors
-
A degree-1 Bezier curve.
-
A degree-2 Bezier curve.
-
A degree-3 Bezier curve.
Errors returned by Bezier helpers.
pub type Error {
SplitOutsideBezier
}
Constructors
-
SplitOutsideBezierThe requested split point is outside the curve’s
0.0..1.0parameter range.
Values
pub fn bezier_bounding_box(curve: BezierData) -> BoundingBox
Return the curve’s exact axis-aligned bounding box over 0.0..1.0.
pub fn bezier_derivative(curve: BezierData, at t: Float) -> Point
Return the derivative with respect to Bezier parameter t.
pub fn bezier_point(curve: BezierData, at t: Float) -> Point
Evaluate a Bezier curve at parameter t.
t is not clamped. 0.0 evaluates the start of the curve, 1.0 evaluates
the end of the curve, and values outside that range extrapolate along the
same polynomial curve.
pub fn cubic_inflection_parameters(
curve: BezierData,
) -> List(Float)
Return the Bezier parameters of a cubic curve’s inflection points.
A cubic Bezier can have up to two inflection points. Values outside
0.0..1.0, values too close to the endpoints, and numerically duplicate
roots are not returned. Splitting at these parameters gives pieces with no
interior inflection, which is often the useful first step before treating
each piece as a convex curve plus its chord. Linear and quadratic curves
return an empty list.
pub fn map_points(
curve: BezierData,
with f: fn(Point) -> Point,
) -> BezierData
Map a Bezier curve’s defining points.
For nonlinear functions, this is not the exact image of every point on the rendered curve. It maps the control polygon and preserves the curve degree.
pub fn split_bezier(
curve: BezierData,
at t: Float,
) -> #(BezierData, BezierData)
Split a Bezier curve at parameter t.
t is not clamped. Values outside 0.0..1.0 extrapolate along the same
polynomial curve, matching bezier_point.
pub fn split_bezier_inside(
curve: BezierData,
at t: Float,
) -> Result(#(BezierData, BezierData), Error)
Split a Bezier curve at parameter t, returning an error outside 0.0..1.0.
Values exactly at 0.0 or 1.0 are accepted and produce one zero-length
curve.
pub fn split_bezier_inside_many(
curve: BezierData,
at points: List(Float),
) -> Result(List(BezierData), Error)
Split a Bezier curve at multiple parameter values, erroring outside 0.0..1.0.
Split points are sorted, exact duplicates are removed, and boundary 0.0
or 1.0 split points are trimmed when they would only create zero-length
boundary curves. Values exactly at 0.0 or 1.0 are accepted.
pub fn split_bezier_many(
curve: BezierData,
at points: List(Float),
) -> List(BezierData)
Split a Bezier curve at multiple parameter values.
Split points are sorted, exact duplicates are removed, and boundary 0.0
or 1.0 split points are trimmed when they would only create zero-length
boundary curves. Values outside 0.0..1.0 are allowed and extrapolate along
the same polynomial curve, matching split_bezier.