Skip to content

VectorCraft: An open-source, vector art tool written by scratch in pure Rust

Cap Liquify dabs while expanding a segment, not only before it (#1708)

MarekSpodBiedryce53bb7c5782
diff --git a/crates/tools/src/distort/liquify.rs b/crates/tools/src/distort/liquify.rs
index 4606be587..de40eef35 100644
--- a/crates/tools/src/distort/liquify.rs
+++ b/crates/tools/src/distort/liquify.rs
@@ -324,7 +324,7 @@ impl Dabber {
self.dabs.push(self.dab(b, pb));
return;
};
- if self.dabs.len() > MAX_DABS {
+ if self.dabs.len() >= MAX_DABS {
return;
}
let len = a.distance(b);
@@ -337,6 +337,9 @@ impl Dabber {
}
let mut s = self.spacing - self.carry;
while s <= len {
+ if self.dabs.len() >= MAX_DABS {
+ return;
+ }
let t = s / len;
self.dabs.push(self.dab(a.lerp(b, t), pa + (pb - pa) * t));
s += self.spacing;
@@ -835,6 +838,19 @@ mod tests {
assert_eq!(dabs(&[Point::new(5.0, 5.0)], 10.0), vec![Point::new(5.0, 5.0)]);
}
+ #[test]
+ fn one_long_segment_stays_within_the_dab_cap() {
+ let mut h = Dabber::with(1.0, Some(1.0), true);
+ for _ in 0..MAX_DABS + 100 {
+ h.push((Point::new(3.0, 3.0), 1.0));
+ }
+ assert_eq!(h.dabs.len(), MAX_DABS);
+ let d = dabs(&[Point::new(0.0, 0.0), Point::new(1.0e9, 0.0)], 1.0);
+ assert!(d.len() <= MAX_DABS + 1, "{} dabs", d.len());
+ let d = dabs(&[Point::new(0.0, 0.0), Point::new(f64::MAX, f64::MAX)], 1.0);
+ assert!(d.len() <= MAX_DABS + 1, "{} dabs", d.len());
+ }
+
#[test]
fn warp_is_deterministic_and_localized() {
let pts = [Point::new(200.0, 100.0), Point::new(240.0, 100.0)];