-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconvexHull.cpp
More file actions
74 lines (52 loc) · 992 Bytes
/
convexHull.cpp
File metadata and controls
74 lines (52 loc) · 992 Bytes
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
#include<bits/stdc++.h>
using namespace std;
class Point {
public :
int x;
int y;
};
bool toChange(Point p, Point q, Point r) {
int x1 = q.x - p.x;
int y1 = q.y - p.y;
int x2 = r.x - q.x;
int y2 = r.y - q.y;
int crossProduct = (x1*y2) - (y1*x2);
return crossProduct < 0;
}
void convexHull(Point *points, int n) {
int left = 0;
for(int i = 1; i < n; i++) {
if(points[i].x < points[left].x) {
left = i;
}
}
vector<Point> hull;
int p = left;
do {
hull.push_back(points[p]);
int q = (p+1)%n;
for(int i = 0; i < n; i++) {
if(toChange(points[p], points[q], points[i])) {
q = i;
}
}
p = q;
} while(p != left);
for(int i = 0; i < hull.size(); i++) {
cout << hull[i].x << " " << hull[i].y << endl;
}
}
int main() {
int n;
cin >> n;
Point *points = new Point[n];
for(int i = 0; i < n; i++) {
cin >> points[i].x;
}
for(int i = 0; i < n; i++) {
cin >> points[i].y;
}
convexHull(points, n);
delete [] points;
return 0;
}