1
1
use super :: poisson_disk:: poisson_disk_sample;
2
2
use crate :: vector:: misc:: dvec2_to_point;
3
3
use glam:: DVec2 ;
4
- use kurbo:: { BezPath , DEFAULT_ACCURACY , Line , ParamCurve , ParamCurveDeriv , PathSeg , Point , Rect , Shape } ;
4
+ use kurbo:: { BezPath , DEFAULT_ACCURACY , Line , ParamCurve , ParamCurveDeriv , PathEl , PathSeg , Point , Rect , Shape } ;
5
5
6
6
pub fn position_on_bezpath ( bezpath : & BezPath , t : f64 , euclidian : bool , segments_length : Option < & [ f64 ] > ) -> Point {
7
7
let ( segment_index, t) = t_value_to_parametric ( bezpath, t, euclidian, segments_length) ;
@@ -21,6 +21,8 @@ pub fn tangent_on_bezpath(bezpath: &BezPath, t: f64, euclidian: bool, segments_l
21
21
pub fn sample_points_on_bezpath ( bezpath : BezPath , spacing : f64 , start_offset : f64 , stop_offset : f64 , adaptive_spacing : bool , segments_length : & [ f64 ] ) -> Option < BezPath > {
22
22
let mut sample_bezpath = BezPath :: new ( ) ;
23
23
24
+ let was_closed = matches ! ( bezpath. elements( ) . last( ) , Some ( PathEl :: ClosePath ) ) ;
25
+
24
26
// Calculate the total length of the collected segments.
25
27
let total_length: f64 = segments_length. iter ( ) . sum ( ) ;
26
28
@@ -50,11 +52,15 @@ pub fn sample_points_on_bezpath(bezpath: BezPath, spacing: f64, start_offset: f6
50
52
return None ;
51
53
}
52
54
55
+ // Decide how many loop-iterations: if closed, skip the last duplicate point
56
+ let sample_count_usize = sample_count as usize ;
57
+ let max_i = if was_closed { sample_count_usize } else { sample_count_usize + 1 } ;
58
+
53
59
// Generate points along the path based on calculated intervals.
54
60
let mut length_up_to_previous_segment = 0. ;
55
61
let mut next_segment_index = 0 ;
56
62
57
- for count in 0 ..=sample_count as usize {
63
+ for count in 0 ..max_i {
58
64
let fraction = count as f64 / sample_count;
59
65
let length_up_to_next_sample_point = fraction * used_length + start_offset;
60
66
let mut next_length = length_up_to_next_sample_point - length_up_to_previous_segment;
@@ -84,6 +90,10 @@ pub fn sample_points_on_bezpath(bezpath: BezPath, spacing: f64, start_offset: f6
84
90
}
85
91
}
86
92
93
+ if was_closed {
94
+ sample_bezpath. close_path ( ) ;
95
+ }
96
+
87
97
Some ( sample_bezpath)
88
98
}
89
99
0 commit comments