@@ -89,6 +89,7 @@ impl LayerSnapper {
89
89
90
90
pub fn free_snap_paths ( & mut self , snap_data : & mut SnapData , point : & SnapCandidatePoint , snap_results : & mut SnapResults , config : SnapTypeConfiguration ) {
91
91
self . collect_paths ( snap_data, !config. use_existing_candidates ) ;
92
+
92
93
let document = snap_data. document ;
93
94
let normals = document. snapping_state . target_enabled ( SnapTarget :: Path ( PathSnapTarget :: NormalToPath ) ) ;
94
95
let tangents = document. snapping_state . target_enabled ( SnapTarget :: Path ( PathSnapTarget :: TangentToPath ) ) ;
@@ -286,21 +287,25 @@ fn snap_normals(path: &SnapCandidatePath, normals: bool, point: &SnapCandidatePo
286
287
}
287
288
}
288
289
289
- // TODO: Snap rectangles and ellipses to ellipses tangents, find out why point.neighbors is empty while drawing rectangles and ellipses
290
+ // TODO: Snap rectangles and ellipses to ellipses tangents.
291
+ // TODO: Find out why `point.neighbors` is empty while drawing rectangles and ellipses.
290
292
fn snap_tangents ( path : & SnapCandidatePath , tangents : bool , point : & SnapCandidatePoint , tolerance : f64 , snap_results : & mut SnapResults ) {
291
293
if !tangents || point. neighbors . len ( ) != 1 {
292
294
return ;
293
295
}
296
+
294
297
let neighbor = point. neighbors [ 0 ] ;
298
+
295
299
for t in path. document_curve . tangents_to_point ( neighbor) {
296
300
let tangent_point = path. document_curve . evaluate ( TValue :: Parametric ( t) ) ;
297
301
298
- if let Some ( closest_point ) = closest_point_along_line ( neighbor, point. document_point , & path. document_curve , tolerance, 20 ) {
302
+ if closest_point_along_line ( neighbor, point. document_point , & path. document_curve , tolerance, 20 ) . is_some ( ) {
299
303
let tangent = ( tangent_point - neighbor) . normalize ( ) ;
300
304
let offset = ( point. document_point - tangent_point) . dot ( tangent) ;
301
305
let snap_to = tangent_point + tangent * offset;
302
306
303
307
let distance = snap_to. distance ( point. document_point ) ;
308
+
304
309
snap_results. points . push ( SnappedPoint {
305
310
snapped_point_document : snap_to,
306
311
source : point. source ,
@@ -316,21 +321,22 @@ fn snap_tangents(path: &SnapCandidatePath, tangents: bool, point: &SnapCandidate
316
321
}
317
322
}
318
323
}
324
+
319
325
fn closest_point_along_line ( start : DVec2 , end : DVec2 , curve : & Bezier , tolerance : f64 , max_iterations : usize ) -> Option < DVec2 > {
320
326
let mut closest_point = None ;
321
327
let mut closest_distance = f64:: INFINITY ;
322
328
323
329
for i in 0 ..=max_iterations {
324
330
let t = i as f64 / max_iterations as f64 ;
325
331
326
- let curve_point = curve. evaluate ( TValue :: Parametric ( t) ) ;
327
- let tangent = curve. tangent ( TValue :: Parametric ( t) ) ;
328
- if tangent. length_squared ( ) == 0.0 {
332
+ if curve. tangent ( TValue :: Parametric ( t) ) . length_squared ( ) == 0. {
329
333
continue ;
330
334
}
331
335
336
+ let curve_point = curve. evaluate ( TValue :: Parametric ( t) ) ;
337
+
332
338
let line_direction = end - start;
333
- if line_direction. length_squared ( ) == 0.0 {
339
+ if line_direction. length_squared ( ) == 0. {
334
340
break ;
335
341
}
336
342
@@ -345,6 +351,7 @@ fn closest_point_along_line(start: DVec2, end: DVec2, curve: &Bezier, tolerance:
345
351
closest_point = Some ( projected_point) ;
346
352
}
347
353
}
354
+
348
355
if closest_distance < tolerance { closest_point } else { None }
349
356
}
350
357
0 commit comments