Skip to content

Commit 77e0277

Browse files
committed
#181 - No more infinite loop. Also uses slope as a Vec2 to encourage SIMD, and rounds results. Guarantees that exit point is the final point.
1 parent ca83f9f commit 77e0277

File tree

2 files changed

+20
-5
lines changed

2 files changed

+20
-5
lines changed

bracket-geometry/src/line_vector.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,4 @@ impl Iterator for VectorLine {
5050
Some(current_point)
5151
}
5252
}
53-
}
53+
}

bracket-geometry/src/lines.rs

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,22 +30,28 @@ pub fn line2d_vector(start: Point, end: Point) -> Vec<Point> {
3030
return vec![start];
3131
}
3232

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);
3535
let n_steps = DistanceAlg::Pythagoras.distance2d(start, end);
36-
let slope = (dest - pos) / n_steps;
36+
let slope = (dest - pos).normalized();
3737
let mut result: Vec<Point> = Vec::with_capacity(n_steps as usize + 1);
38+
let mut count = 0;
3839
result.push(start);
3940
loop {
4041
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);
4243
if result[result.len() - 1] != new_point {
44+
if count == n_steps as i32 {
45+
result.push(end);
46+
break;
47+
}
4348
result.push(new_point);
4449
if new_point == end {
4550
// arrived
4651
break;
4752
}
4853
}
54+
count += 1;
4955
}
5056

5157
result
@@ -162,4 +168,13 @@ mod tests {
162168
]
163169
);
164170
}
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+
}
165180
}

0 commit comments

Comments
 (0)