Skip to content

Commit 9a9597f

Browse files
committed
Fix panic due to wrong mathematical operations
1 parent 1fb26a1 commit 9a9597f

File tree

1 file changed

+15
-9
lines changed

1 file changed

+15
-9
lines changed

editor/src/messages/tool/common_functionality/snapping/layer_snapper.rs

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,12 @@ fn snap_tangents(path: &SnapCandidatePath, tangents: bool, point: &SnapCandidate
296296
let tangent_point = path.document_curve.evaluate(TValue::Parametric(t));
297297

298298
if let Some(closest_point) = closest_point_along_line(neighbor, point.document_point, &path.document_curve, tolerance, 20) {
299-
let tangent = (tangent_point - neighbor).normalize();
299+
let tangent_vector = tangent_point - neighbor;
300+
if tangent_vector.length_squared() < f64::EPSILON {
301+
continue;
302+
}
303+
let tangent = tangent_vector.normalize();
304+
300305
let offset = (point.document_point - tangent_point).dot(tangent);
301306
let snap_to = tangent_point + tangent * offset;
302307

@@ -320,23 +325,24 @@ fn closest_point_along_line(start: DVec2, end: DVec2, curve: &Bezier, tolerance:
320325
let mut closest_point = None;
321326
let mut closest_distance = f64::INFINITY;
322327

328+
let line_direction = end - start;
329+
if line_direction.length_squared() < f64::EPSILON {
330+
return None;
331+
}
332+
let line_direction_normalized = line_direction.normalize();
333+
323334
for i in 0..=max_iterations {
324335
let t = i as f64 / max_iterations as f64;
325336

326337
let curve_point = curve.evaluate(TValue::Parametric(t));
327338
let tangent = curve.tangent(TValue::Parametric(t));
328-
if tangent.length_squared() == 0.0 {
339+
if tangent.length_squared() < f64::EPSILON {
329340
continue;
330341
}
331342

332-
let line_direction = end - start;
333-
if line_direction.length_squared() == 0.0 {
334-
break;
335-
}
336-
337343
let v = curve_point - start;
338-
let projected_distance = v.dot(line_direction.normalize());
339-
let projected_point = start + projected_distance * line_direction.normalize();
344+
let projected_distance = v.dot(line_direction_normalized);
345+
let projected_point = start + projected_distance * line_direction_normalized;
340346

341347
let distance = projected_point.distance(curve_point);
342348

0 commit comments

Comments
 (0)