-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
Copy pathjarvis_scan.rs
193 lines (176 loc) · 6.09 KB
/
jarvis_scan.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
use crate::geometry::Point;
use crate::geometry::Segment;
// Returns a Vec of Points that make up the convex hull of `points`. Returns an empty Vec if there
// is no convex hull.
pub fn jarvis_march(points: Vec<Point>) -> Vec<Point> {
if points.len() <= 2 {
return vec![];
}
let mut convex_hull = vec![];
let mut left_point = 0;
for i in 1..points.len() {
// Find the initial point, which is the leftmost point. In the case of a tie, we take the
// bottom-most point. This helps prevent adding colinear points on the last segment to the hull.
if points[i].x < points[left_point].x
|| (points[i].x == points[left_point].x && points[i].y < points[left_point].y)
{
left_point = i;
}
}
convex_hull.push(points[left_point].clone());
let mut p = left_point;
loop {
// Find the next counter-clockwise point.
let mut next_p = (p + 1) % points.len();
for i in 0..points.len() {
let orientation = points[p].consecutive_orientation(&points[i], &points[next_p]);
if orientation > 0.0 {
next_p = i;
}
}
if next_p == left_point {
// Completed constructing the hull. Exit the loop.
break;
}
p = next_p;
let last = convex_hull.len() - 1;
if convex_hull.len() > 1
&& Segment::from_points(points[p].clone(), convex_hull[last - 1].clone())
.on_segment(&convex_hull[last])
{
// If the last point lies on the segment with the new point and the second to last
// point, we can remove the last point from the hull.
convex_hull[last] = points[p].clone();
} else {
convex_hull.push(points[p].clone());
}
}
if convex_hull.len() <= 2 {
return vec![];
}
let last = convex_hull.len() - 1;
if Segment::from_points(convex_hull[0].clone(), convex_hull[last - 1].clone())
.on_segment(&convex_hull[last])
{
// Check for the edge case where the last point lies on the segment with the zero'th and
// second the last point. In this case, we remove the last point from the hull.
convex_hull.pop();
if convex_hull.len() == 2 {
return vec![];
}
}
convex_hull
}
#[cfg(test)]
mod tests {
use super::jarvis_march;
use super::Point;
fn test_jarvis(convex_hull: Vec<Point>, others: Vec<Point>) {
let mut points = others.clone();
points.append(&mut convex_hull.clone());
let jarvis = jarvis_march(points);
for point in convex_hull {
assert!(jarvis.contains(&point));
}
for point in others {
assert!(!jarvis.contains(&point));
}
}
#[test]
fn too_few_points() {
test_jarvis(vec![], vec![]);
test_jarvis(vec![], vec![Point::new(0.0, 0.0)]);
}
#[test]
fn duplicate_point() {
let p = Point::new(0.0, 0.0);
test_jarvis(vec![], vec![p.clone(), p.clone(), p.clone(), p.clone(), p]);
}
#[test]
fn points_same_line() {
let p1 = Point::new(1.0, 0.0);
let p2 = Point::new(2.0, 0.0);
let p3 = Point::new(3.0, 0.0);
let p4 = Point::new(4.0, 0.0);
let p5 = Point::new(5.0, 0.0);
// let p6 = Point::new(1.0, 1.0);
test_jarvis(vec![], vec![p1, p2, p3, p4, p5]);
}
#[test]
fn triangle() {
let p1 = Point::new(1.0, 1.0);
let p2 = Point::new(2.0, 1.0);
let p3 = Point::new(1.5, 2.0);
let points = vec![p1, p2, p3];
test_jarvis(points, vec![]);
}
#[test]
fn rectangle() {
let p1 = Point::new(1.0, 1.0);
let p2 = Point::new(2.0, 1.0);
let p3 = Point::new(2.0, 2.0);
let p4 = Point::new(1.0, 2.0);
let points = vec![p1, p2, p3, p4];
test_jarvis(points, vec![]);
}
#[test]
fn triangle_with_points_in_middle() {
let p1 = Point::new(1.0, 1.0);
let p2 = Point::new(2.0, 1.0);
let p3 = Point::new(1.5, 2.0);
let p4 = Point::new(1.5, 1.5);
let p5 = Point::new(1.2, 1.3);
let p6 = Point::new(1.8, 1.2);
let p7 = Point::new(1.5, 1.9);
let hull = vec![p1, p2, p3];
let others = vec![p4, p5, p6, p7];
test_jarvis(hull, others);
}
#[test]
fn rectangle_with_points_in_middle() {
let p1 = Point::new(1.0, 1.0);
let p2 = Point::new(2.0, 1.0);
let p3 = Point::new(2.0, 2.0);
let p4 = Point::new(1.0, 2.0);
let p5 = Point::new(1.5, 1.5);
let p6 = Point::new(1.2, 1.3);
let p7 = Point::new(1.8, 1.2);
let p8 = Point::new(1.9, 1.7);
let p9 = Point::new(1.4, 1.9);
let hull = vec![p1, p2, p3, p4];
let others = vec![p5, p6, p7, p8, p9];
test_jarvis(hull, others);
}
#[test]
fn star() {
// A single stroke star shape (kind of). Only the tips(p1-5) are part of the convex hull. The
// other points would create angles >180 degrees if they were part of the polygon.
let p1 = Point::new(-5.0, 6.0);
let p2 = Point::new(-11.0, 0.0);
let p3 = Point::new(-9.0, -8.0);
let p4 = Point::new(4.0, 4.0);
let p5 = Point::new(6.0, -7.0);
let p6 = Point::new(-7.0, -2.0);
let p7 = Point::new(-2.0, -4.0);
let p8 = Point::new(0.0, 1.0);
let p9 = Point::new(1.0, 0.0);
let p10 = Point::new(-6.0, 1.0);
let hull = vec![p1, p2, p3, p4, p5];
let others = vec![p6, p7, p8, p9, p10];
test_jarvis(hull, others);
}
#[test]
fn rectangle_with_points_on_same_line() {
let p1 = Point::new(1.0, 1.0);
let p2 = Point::new(2.0, 1.0);
let p3 = Point::new(2.0, 2.0);
let p4 = Point::new(1.0, 2.0);
let p5 = Point::new(1.5, 1.0);
let p6 = Point::new(1.0, 1.5);
let p7 = Point::new(2.0, 1.5);
let p8 = Point::new(1.5, 2.0);
let hull = vec![p1, p2, p3, p4];
let others = vec![p5, p6, p7, p8];
test_jarvis(hull, others);
}
}