Struct CubicBezier
Represents a cubic Bézier curve defined by four control points.
public readonly struct CubicBezier : ICurve
- Implements
- Inherited Members
Remarks
A cubic Bézier curve provides smooth interpolation between P0 and P3, influenced by the control points P1 and P2. This is commonly used in vector graphics, animation timing, and layout paths.
Constructors
CubicBezier(Point, Point, Point, Point)
Initializes a new cubic Bézier curve with the specified control points.
public CubicBezier(Point p0, Point p1, Point p2, Point p3)
Parameters
Fields
P0
The starting point of the curve.
public readonly Point P0
Field Value
P1
The first control point, influencing the curve near P0.
public readonly Point P1
Field Value
P2
The second control point, influencing the curve near P3.
public readonly Point P2
Field Value
P3
The ending point of the curve.
public readonly Point P3
Field Value
Properties
this[NFloat]
Gets the interpolated point on the curve at the specified parameter.
public Point this[NFloat t] { get; }
Parameters
t
NFloatA normalized parameter in the range [0, 1].
Property Value
QuadraticApproximation
public QuadraticBezier QuadraticApproximation { get; }
Property Value
Methods
ClosestT(Point)
Returns the parameter t
∈ [0, 1] at which the curve is closest to the specified target
point.
This version uses uniform sampling with 16 segments and is fast enough for interactive use.
public NFloat ClosestT(Point target)
Parameters
target
PointThe point to compare against the curve.
Returns
- NFloat
The approximate
t
value where the curve is closest totarget
.
ClosestT(Point, NFloat)
Returns the parameter t
∈ [0, 1] at which the curve is closest to the specified target
point,
using recursive ternary subdivision until the difference in t
range is less than precision
.
public NFloat ClosestT(Point target, NFloat precision)
Parameters
target
PointThe point to compare against the curve.
precision
NFloatThe minimum resolution for
t
convergence. Smaller values yield more accurate results.
Returns
- NFloat
The
t
value where the curve is closest totarget
within the specifiedprecision
.
Drag(NFloat, Vector)
Returns a new CubicBezier curve where the control points P1
and P2
are adjusted based on a drag gesture at a given parametric position t
.
public CubicBezier Drag(NFloat t, Vector delta)
Parameters
t
NFloatA normalized value (between 0 and 1) representing the position on the curve where the drag occurs.
delta
VectorThe drag vector representing how far the user dragged the point at
t
.
Returns
- CubicBezier
A new CubicBezier with
P0
andP3
unchanged, andP1
andP2
adjusted proportionally to the drag direction and distance.
Remarks
The influence of the drag on P1
and P2
is weighted quadratically based on their
distance from t
along the curve:
P1
is influenced by (1 - t)²
and P2
by t²
.
This preserves the endpoints while allowing intuitive manipulation of the curve shape.
DragAt(Point, Vector)
Returns a new cubic Bézier curve with adjusted control points so the point on the curve
nearest to origin
is moved by delta
.
public CubicBezier DragAt(Point origin, Vector delta)
Parameters
Returns
DragAt(Point, Vector, NFloat)
Returns a new cubic Bézier curve with adjusted control points so the point on the curve
nearest to origin
is moved by delta
.
Uses recursive refinement based on the specified precision
.
public CubicBezier DragAt(Point origin, Vector delta, NFloat precision)
Parameters
Returns
Length()
Approximates the arc length of the curve using uniform sampling with 16 segments.
public NFloat Length()
Returns
Length(NFloat)
Approximates the arc length of the curve using recursive adaptive subdivision.
public NFloat Length(NFloat precision)
Parameters
precision
NFloatThe tolerance value used to determine when a segment is flat enough to stop subdividing. Smaller values result in more accurate results but require more computation.
Returns
Lerp(NFloat)
Computes the interpolated point on the curve at parameter t
using De Casteljau’s algorithm.
public Point Lerp(NFloat t)
Parameters
t
NFloatA normalized parameter in the range [0, 1].
Returns
SplitIntoYMonotonicCurves()
public MonotonicCubicBezier SplitIntoYMonotonicCurves()
Returns
Subdivide(NFloat)
Subdivides this cubic Bézier curve at parameter t, returning two curves.
public (CubicBezier, CubicBezier) Subdivide(NFloat t)
Parameters
t
NFloat
Returns
Tangent(NFloat)
Computes the tangent vector of the curve at parameter t
.
public Vector Tangent(NFloat t)
Parameters
t
NFloatA normalized parameter in the range [0, 1].
Returns
ToQuadratics(Span<QuadraticBezier>, NFloat, out int)
Converts this cubic Bézier curve into a sequence of Y-monotonic quadratic Bézier segments, each approximating a portion of the original curve within the specified flatness precision.
public void ToQuadratics(Span<QuadraticBezier> buffer, NFloat precision, out int count)
Parameters
buffer
Span<QuadraticBezier>A span to receive the quadratic segments. Must be large enough to hold the result. A typical size is between 4 and 32. Must be at least 3 to accommodate the initial monotonic split.
precision
NFloatThe maximum allowed distance between the original cubic curve and its quadratic approximation. Lower values yield more accurate approximations but may require more output segments.
count
intThe number of segments written to
buffer
.
Remarks
This method uses an adaptive, flatness-aware subdivision strategy. It begins by splitting the
cubic curve into up to 3 Y-monotonic segments. Each is then approximated with a quadratic Bézier,
and the segments with the highest deviation are recursively subdivided until the total number of
segments fits within buffer
and all segments meet the precision requirement.
The output segments are returned in order, forming a continuous piecewise-curve that follows the original cubic. Suitable for SDF tessellation or scanline-based rasterization.
Exceptions
- ArgumentException
Thrown if
buffer
has fewer than 3 elements.
Operators
implicit operator CubicBezier((Point p0, Point p1, Point p2, Point p3))
Implicitly converts a tuple of four points into a CubicBezier.
public static implicit operator CubicBezier((Point p0, Point p1, Point p2, Point p3) value)
Parameters
value
(Point p0, Point p1, Point p2, Point p3)A tuple representing the start point, two control points, and end point.
Returns
implicit operator CubicBezier(CubicSpline)
Implicitly converts this cubic Catmull–Rom spline to a CubicBezier.
public static implicit operator CubicBezier(CubicSpline spline)
Parameters
spline
CubicSpline
Returns
Remarks
Uses the canonical Catmull–Rom to Bézier mapping, interpolating between P1 and P2.
implicit operator CubicBezier(QuadraticBezier)
Implicitly converts a QuadraticBezier to a CubicBezier using interpolation for smooth parameterization.
public static implicit operator CubicBezier(QuadraticBezier quadratic)
Parameters
quadratic
QuadraticBezier
Returns
Remarks
This conversion evaluates the quadratic Bézier at fixed steps (1⁄3 and 2⁄3) to generate the internal control points of the cubic, resulting in a visually smooth upgrade path.