pub struct Bezier<P, const N: usize>where
P: Point,{ /* private fields */ }
Expand description
General implementation of a Bezier curve of arbitrary degree (= number of control points - 1). The curve is solely defined by an array of ‘control_points’. The degree is defined as degree = control_points.len() - 1. Points on the curve can be evaluated with an interpolation parameter ‘t’ in interval [0,1] using the eval() and eval_casteljau() methods. Generic parameters: P: Generic points ‘P’ as defined by there Point trait const generic parameters: N: Number of control points
Implementations§
source§impl<P, const N: usize> Bezier<P, { N }>where
P: Point,
impl<P, const N: usize> Bezier<P, { N }>where P: Point,
sourcepub fn new(control_points: [P; N]) -> Bezier<P, { N }>
pub fn new(control_points: [P; N]) -> Bezier<P, { N }>
Create a new Bezier curve that interpolates the control_points
. The degree is defined as degree = control_points.len() - 1.
Desired curve must have a valid number of control points and knots in relation to its degree or the constructor will return None.
A B-Spline curve requires at least one more control point than the degree (control_points.len() > degree
) and the number of knots should be equal to control_points.len() + degree + 1
.
pub fn control_points(&self) -> [P; N]
sourcepub fn eval(&self, t: P::Scalar) -> P
pub fn eval(&self, t: P::Scalar) -> P
Evaluate a point on the curve at point ‘t’ which should be in the interval [0,1] This is implemented using De Casteljau’s algorithm (over a temporary array with const generic sizing)
sourcepub fn distance_to_point(&self, point: P) -> P::Scalar
pub fn distance_to_point(&self, point: P) -> P::Scalar
Calculates the minimum distance between given ‘point’ and the curve. Uses two passes with the same amount of steps in t:
- coarse search over the whole curve
- fine search around the minimum yielded by the coarse search
pub fn split(&self, t: P::Scalar) -> (Self, Self)
sourcepub fn derivative(&self) -> Bezier<P, { _ }>
pub fn derivative(&self) -> Bezier<P, { _ }>
Returns the derivative curve of self which has N-1 control points. The derivative of an nth degree Bézier curve is an (n-1)th degree Bézier curve, with one fewer term, and new weights w0…wn-1 derived from the original weights as n(wi+1 - wi). So for a 3rd degree curve, with four weights, the derivative has three new weights: w0 = 3(w1-w0), w’1 = 3(w2-w1) and w’2 = 3(w3-w2).
sourcepub fn arclen(&self, nsteps: usize) -> P::Scalar
pub fn arclen(&self, nsteps: usize) -> P::Scalar
Approximates the arc length of the curve by flattening it with straight line segments. This works quite well, at ~32 segments it should already provide an error in the decimal places The accuracy gain falls off with more steps so this approximation is unfeasable if desired accuracy is greater than 1-2 decimal places