slight bezier optimization
This commit is contained in:
+6
-9
@@ -44,19 +44,16 @@ impl<'p> Curve<'p> {
|
||||
}
|
||||
|
||||
let mut start = 0;
|
||||
let mut end = 0;
|
||||
let mut result = Vec::with_capacity(4);
|
||||
let mut result = Vec::new();
|
||||
|
||||
for i in 0..points.len() - 1 {
|
||||
if end - start > 1 && points[i] == points[end - 1] {
|
||||
for (end, (curr, next)) in (1..).zip(points.iter().zip(points.iter().skip(1))) {
|
||||
if end - start > 1 && curr == next {
|
||||
Self::_bezier(&mut result, &points[start..end]);
|
||||
start = end;
|
||||
}
|
||||
|
||||
end += 1;
|
||||
}
|
||||
|
||||
Self::_bezier(&mut result, &points[start..end + 1]);
|
||||
Self::_bezier(&mut result, &points[start..]);
|
||||
|
||||
Self::Bezier(Points::Multi(result))
|
||||
}
|
||||
@@ -67,10 +64,10 @@ impl<'p> Curve<'p> {
|
||||
let n = points.len() as i32 - 1;
|
||||
|
||||
while i < 1.0 + step {
|
||||
let point = (0..=n).fold(Pos2 { x: 0.0, y: 0.0 }, |point, p| {
|
||||
let point = (0..).zip(points).fold(Pos2::zero(), |point, (p, curr)| {
|
||||
let factor = math_util::cpn(p, n) * (1.0 - i).powi(n - p) * i.powi(p);
|
||||
|
||||
point + points[p as usize] * factor
|
||||
point + *curr * factor
|
||||
});
|
||||
|
||||
result.push(point);
|
||||
|
||||
+1
-1
@@ -58,7 +58,7 @@ pub(crate) fn point_on_lines(points: &[Pos2], len: f32) -> Pos2 {
|
||||
|
||||
pub(crate) fn point_at_distance(points: &[Pos2], dist: f32) -> Pos2 {
|
||||
if points.len() < 2 {
|
||||
return Pos2 { x: 0.0, y: 0.0 };
|
||||
return Pos2::zero();
|
||||
} else if dist.abs() <= f32::EPSILON {
|
||||
return points[0];
|
||||
}
|
||||
|
||||
@@ -9,6 +9,11 @@ pub struct Pos2 {
|
||||
}
|
||||
|
||||
impl Pos2 {
|
||||
#[inline]
|
||||
pub fn zero() -> Self {
|
||||
Self::default()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn length_squared(&self) -> f32 {
|
||||
self.dot(*self)
|
||||
|
||||
Reference in New Issue
Block a user