@@ -296,7 +296,12 @@ fn snap_tangents(path: &SnapCandidatePath, tangents: bool, point: &SnapCandidate
296
296
let tangent_point = path. document_curve . evaluate ( TValue :: Parametric ( t) ) ;
297
297
298
298
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
+
300
305
let offset = ( point. document_point - tangent_point) . dot ( tangent) ;
301
306
let snap_to = tangent_point + tangent * offset;
302
307
@@ -320,23 +325,24 @@ fn closest_point_along_line(start: DVec2, end: DVec2, curve: &Bezier, tolerance:
320
325
let mut closest_point = None ;
321
326
let mut closest_distance = f64:: INFINITY ;
322
327
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
+
323
334
for i in 0 ..=max_iterations {
324
335
let t = i as f64 / max_iterations as f64 ;
325
336
326
337
let curve_point = curve. evaluate ( TValue :: Parametric ( t) ) ;
327
338
let tangent = curve. tangent ( TValue :: Parametric ( t) ) ;
328
- if tangent. length_squared ( ) == 0.0 {
339
+ if tangent. length_squared ( ) < f64 :: EPSILON {
329
340
continue ;
330
341
}
331
342
332
- let line_direction = end - start;
333
- if line_direction. length_squared ( ) == 0.0 {
334
- break ;
335
- }
336
-
337
343
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 ;
340
346
341
347
let distance = projected_point. distance ( curve_point) ;
342
348
0 commit comments