pub struct BezierSpline<T>(/* private fields */);Expand description
A curve composed of one or more concatenated cubic Bézier curves.
Implementations§
Source§impl<T> BezierSpline<T>
impl<T> BezierSpline<T>
Sourcepub fn new(pts: &[T]) -> Self
pub fn new(pts: &[T]) -> Self
Creates a Bézier spline from the given control points. The number of
elements in pts must be 3n + 1 for some positive integer n.
Consecutive points in pts make up Bézier curves such that:
pts[0..=3]define the first curve,pts[3..=6]define the second curve,
and so on.
§Panics
If pts.len() < 4 or if pts.len() % 3 != 1.
Sourcepub fn from_rays<I>(rays: I) -> Selfwhere
I: IntoIterator<Item = Ray<T>>,
pub fn from_rays<I>(rays: I) -> Selfwhere
I: IntoIterator<Item = Ray<T>>,
Constructs a Bézier spline
Sourcepub fn eval(&self, t: f32) -> T
pub fn eval(&self, t: f32) -> T
Evaluates self at position t.
Returns the first point if t < 0 and the last point if t > 1.
Sourcepub fn tangent(&self, t: f32) -> T::Diff
pub fn tangent(&self, t: f32) -> T::Diff
Returns the tangent of self at t.
Clamps t to the range [0, 1].
Sourcepub fn approximate(&self, halt: impl Fn(&T::Diff) -> bool) -> Polyline<T>
pub fn approximate(&self, halt: impl Fn(&T::Diff) -> bool) -> Polyline<T>
Approximates self as a sequence of line segments.
Recursively subdivides the curve into two half-curves, stopping once
the approximation error is small enough, as determined by the halt
function.
Given a curve segment between some points p and r, the parameter
passed to halt is the distance to the real midpoint q from its
linear approximation q'. If halt returns true, the line segment
pr is returned as the approximation of this curve segment, otherwise
the bisection continues.
Note that this heuristic does not work well in certain edge cases
(consider, for example, an S-shaped curve where q' is very close
to q, yet a straight line would be a poor approximation). However,
in practice it tends to give reasonable results.
___--- q ---___
_--´ | `--_
_--´ | `--_
_-p ------------- q' ------------ r-_
_-´ `-_§Examples
use retrofire_core::math::{BezierSpline, vec2, Vec2};
let curve = BezierSpline::<Vec2>::new(
&[vec2(0.0, 0.0), vec2(0.0, 1.0), vec2(1.0, 1.0), vec2(1.0, 0.0)]
);
let approx = curve.approximate(|err| err.len_sqr() < 0.01*0.01);
assert_eq!(approx.0.len(), 17);Trait Implementations§
Source§impl<T: Clone> Clone for BezierSpline<T>
impl<T: Clone> Clone for BezierSpline<T>
Source§fn clone(&self) -> BezierSpline<T>
fn clone(&self) -> BezierSpline<T>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more