@@ -30,22 +30,28 @@ pub fn line2d_vector(start: Point, end: Point) -> Vec<Point> {
30
30
return vec ! [ start] ;
31
31
}
32
32
33
- let mut pos = Vec2 :: new ( start. x as f32 + 0.5 , start. y as f32 + 0.5 ) ;
34
- let dest = Vec2 :: new ( end. x as f32 + 0.5 , end. y as f32 + 0.5 ) ;
33
+ let mut pos = Vec2 :: new ( start. x as f32 , start. y as f32 ) ;
34
+ let dest = Vec2 :: new ( end. x as f32 , end. y as f32 ) ;
35
35
let n_steps = DistanceAlg :: Pythagoras . distance2d ( start, end) ;
36
- let slope = ( dest - pos) / n_steps ;
36
+ let slope = ( dest - pos) . normalized ( ) ;
37
37
let mut result: Vec < Point > = Vec :: with_capacity ( n_steps as usize + 1 ) ;
38
+ let mut count = 0 ;
38
39
result. push ( start) ;
39
40
loop {
40
41
pos += slope;
41
- let new_point = Point :: new ( pos. x as i32 , pos. y as i32 ) ;
42
+ let new_point = Point :: new ( pos. x . round ( ) as i32 , pos. y . round ( ) as i32 ) ;
42
43
if result[ result. len ( ) - 1 ] != new_point {
44
+ if count == n_steps as i32 {
45
+ result. push ( end) ;
46
+ break ;
47
+ }
43
48
result. push ( new_point) ;
44
49
if new_point == end {
45
50
// arrived
46
51
break ;
47
52
}
48
53
}
54
+ count += 1 ;
49
55
}
50
56
51
57
result
@@ -162,4 +168,13 @@ mod tests {
162
168
]
163
169
) ;
164
170
}
171
+
172
+ #[ test]
173
+ pub fn infinite_loop_bug181 ( ) {
174
+ let pt = Point { x : 2 , y : 2 } ;
175
+ let pt2 = Point { x : 7 , y : -4 } ;
176
+ let line = line2d_vector ( pt, pt2) ;
177
+
178
+ assert_eq ! ( line[ line. len( ) -1 ] , pt2) ;
179
+ }
165
180
}
0 commit comments