forked from DanielW48/HackPack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConvexHull.java
More file actions
72 lines (63 loc) · 2.05 KB
/
Copy pathConvexHull.java
File metadata and controls
72 lines (63 loc) · 2.05 KB
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
class ConvexHull {
static int n;
static point[] arr;
static point start;
public static void main (String[] args){
arr = new point[n];
point start = arr[0];
for(int i = 1; i < n; ++i){
if(arr[i].y < start.y || (arr[i].y == start.y && arr[i].x < start.x)) start = arr[i];
}
Arrays.sort(arr);
ArrayDeque<point> stk = new ArrayDeque<>();
stk.push(arr[0]);
stk.push(arr[1]);
floop : for(int i = 2; i < n; ++i){
point curr = arr[i];
point mid = stk.pop();
point prev = stk.pop();
while(crossProd(prev, mid, curr) <= 0){
if(stk.isEmpty()){
stk.push(prev);
stk.push(curr);
continue floop;
}
mid = prev;
prev = stk.pop();
}
stk.push(prev);
stk.push(mid);
stk.push(curr);
}
double perim = 0;
point prev = start;
for(point curr : stk){
perim += prev.d(curr);
prev = curr;
}
}
//if cross product > 0, then AC is to the left of AB
static int crossProd(point a, point b, point c){
return (a.x - b.x) * (a.y - c.y) - (a.x - c.x) * (a.y - b.y);
}
static class point implements Comparable<point> {
int x, y;
point(int xx, int yy){
x = xx;
y = yy;
}
double d(point in){
return Math.sqrt(Math.pow(x - in.x, 2) + Math.pow(y - in.y, 2));
}
public int compareTo(point in){
if(x == start.x && y == start.y) return -1;
if(in.x == start.x && in.y == start.y) return 1;
int cp = crossProd(start, in, this);
if(cp != 0) return cp;
return Double.compare(start.d(this), start.d(in));
}
public String toString(){
return x + " " + y;
}
}
}