11use super :: poisson_disk:: poisson_disk_sample;
22use crate :: vector:: misc:: dvec2_to_point;
33use 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 } ;
55
66pub fn position_on_bezpath ( bezpath : & BezPath , t : f64 , euclidian : bool , segments_length : Option < & [ f64 ] > ) -> Point {
77 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
2121pub fn sample_points_on_bezpath ( bezpath : BezPath , spacing : f64 , start_offset : f64 , stop_offset : f64 , adaptive_spacing : bool , segments_length : & [ f64 ] ) -> Option < BezPath > {
2222 let mut sample_bezpath = BezPath :: new ( ) ;
2323
24+ let was_closed = matches ! ( bezpath. elements( ) . last( ) , Some ( PathEl :: ClosePath ) ) ;
25+
2426 // Calculate the total length of the collected segments.
2527 let total_length: f64 = segments_length. iter ( ) . sum ( ) ;
2628
@@ -50,11 +52,15 @@ pub fn sample_points_on_bezpath(bezpath: BezPath, spacing: f64, start_offset: f6
5052 return None ;
5153 }
5254
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+
5359 // Generate points along the path based on calculated intervals.
5460 let mut length_up_to_previous_segment = 0. ;
5561 let mut next_segment_index = 0 ;
5662
57- for count in 0 ..=sample_count as usize {
63+ for count in 0 ..max_i {
5864 let fraction = count as f64 / sample_count;
5965 let length_up_to_next_sample_point = fraction * used_length + start_offset;
6066 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
8490 }
8591 }
8692
93+ if was_closed {
94+ sample_bezpath. close_path ( ) ;
95+ }
96+
8797 Some ( sample_bezpath)
8898}
8999
0 commit comments