@@ -34,7 +34,7 @@ pub fn sample_points_on_bezpath(bezpath: BezPath, spacing: f64, start_offset: f6
34
34
}
35
35
36
36
// Determine the number of points to generate along the path.
37
- let count = if adaptive_spacing {
37
+ let sample_count = if adaptive_spacing {
38
38
// Calculate point count to evenly distribute points while covering the entire path.
39
39
// With adaptive spacing, we widen or narrow the points as necessary to ensure the last point is always at the end of the path.
40
40
( used_length / spacing) . round ( )
@@ -48,28 +48,28 @@ pub fn sample_points_on_bezpath(bezpath: BezPath, spacing: f64, start_offset: f6
48
48
} ;
49
49
50
50
// Skip if there are no points to generate.
51
- if count < 1. {
51
+ if sample_count < 1. {
52
52
return None ;
53
53
}
54
54
// Generate points along the path based on calculated intervals.
55
- let max_c = count as usize ;
56
55
let mut length_upto_previous_segment = 0. ;
57
56
let mut next_segment_index = 0 ;
58
57
59
- for c in 0 ..=max_c {
60
- let fraction = c as f64 / count ;
61
- let c_next_length = fraction * used_length + start_offset;
62
- let mut next_length = c_next_length - length_upto_previous_segment;
58
+ for count in 0 ..=sample_count as usize {
59
+ let fraction = count as f64 / sample_count ;
60
+ let length_upto_next_sample_point = fraction * used_length + start_offset;
61
+ let mut next_length = length_upto_next_sample_point - length_upto_previous_segment;
63
62
let mut next_segment_length = segments_length[ next_segment_index] ;
64
63
65
- while next_length > next_segment_length && ( next_length - next_segment_length ) > 1e-7 {
66
- next_segment_index += 1 ;
64
+ // Keep moving to the next segment while the next sample point length is less or equals to the length upto that segment.
65
+ while next_length > next_segment_length && ( next_length - next_segment_length ) > POSITION_ACCURACY {
67
66
length_upto_previous_segment += next_segment_length;
68
- next_length = c_next_length - length_upto_previous_segment;
67
+ next_length = length_upto_next_sample_point - length_upto_previous_segment;
68
+ next_segment_index += 1 ;
69
69
next_segment_length = segments_length[ next_segment_index] ;
70
70
}
71
71
72
- let t = next_length / next_segment_length;
72
+ let t = ( next_length / next_segment_length) . clamp ( 0. , 1. ) ;
73
73
74
74
let point = bezpath. get_seg ( next_segment_index + 1 ) . unwrap ( ) . eval ( t) ;
75
75
0 commit comments