slight bezier optimization

This commit is contained in:
MaxOhn
2021-04-28 23:21:40 +02:00
parent 964b972eac
commit 921f2afeac
3 changed files with 12 additions and 10 deletions
+6 -9
View File
@@ -44,19 +44,16 @@ impl<'p> Curve<'p> {
} }
let mut start = 0; let mut start = 0;
let mut end = 0; let mut result = Vec::new();
let mut result = Vec::with_capacity(4);
for i in 0..points.len() - 1 { for (end, (curr, next)) in (1..).zip(points.iter().zip(points.iter().skip(1))) {
if end - start > 1 && points[i] == points[end - 1] { if end - start > 1 && curr == next {
Self::_bezier(&mut result, &points[start..end]); Self::_bezier(&mut result, &points[start..end]);
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)) Self::Bezier(Points::Multi(result))
} }
@@ -67,10 +64,10 @@ impl<'p> Curve<'p> {
let n = points.len() as i32 - 1; let n = points.len() as i32 - 1;
while i < 1.0 + step { 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); 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); result.push(point);
+1 -1
View File
@@ -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 { pub(crate) fn point_at_distance(points: &[Pos2], dist: f32) -> Pos2 {
if points.len() < 2 { if points.len() < 2 {
return Pos2 { x: 0.0, y: 0.0 }; return Pos2::zero();
} else if dist.abs() <= f32::EPSILON { } else if dist.abs() <= f32::EPSILON {
return points[0]; return points[0];
} }
+5
View File
@@ -9,6 +9,11 @@ pub struct Pos2 {
} }
impl Pos2 { impl Pos2 {
#[inline]
pub fn zero() -> Self {
Self::default()
}
#[inline] #[inline]
pub fn length_squared(&self) -> f32 { pub fn length_squared(&self) -> f32 {
self.dot(*self) self.dot(*self)