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:

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

  • LinearBezierData(start: Point, end: Point)

    A degree-1 Bezier curve.

  • QuadraticBezierData(start: Point, control: Point, end: Point)

    A degree-2 Bezier curve.

  • CubicBezierData(
      start: Point,
      control1: Point,
      control2: Point,
      end: Point,
    )

    A degree-3 Bezier curve.

An axis-aligned bounding box for a Bezier curve.

pub type BoundingBox {
  BoundingBox(min: Point, max: Point)
}

Constructors

Errors returned by Bezier helpers.

pub type Error {
  SplitOutsideBezier
}

Constructors

  • SplitOutsideBezier

    The requested split point is outside the curve’s 0.0..1.0 parameter range.

A lightweight point used by the Bezier math helpers.

pub type Point {
  Point(x: Float, y: Float)
}

Constructors

  • Point(x: Float, y: Float)

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_end(curve: BezierData) -> Point

Return the curve’s end point.

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 bezier_start(curve: BezierData) -> Point

Return the curve’s start point.

pub fn cubic_bezier_data(
  start start: Point,
  control1 control1: Point,
  control2 control2: Point,
  end end: Point,
) -> BezierData

Create cubic Bezier data.

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 linear_bezier_data(
  start start: Point,
  end end: Point,
) -> BezierData

Create linear Bezier data.

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 quadratic_bezier_data(
  start start: Point,
  control control: Point,
  end end: Point,
) -> BezierData

Create quadratic Bezier data.

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.

Search Document