forked from algorithm-archivists/algorithm-archive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraham_scan.cpp
84 lines (76 loc) · 2.25 KB
/
graham_scan.cpp
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
#include <algorithm>
#include <cmath>
#include <iostream>
#include <vector>
struct point {
double x;
double y;
};
std::ostream& operator<<(std::ostream& os, const std::vector<point>& points) {
for (auto p : points) {
os << "(" << p.x << ", " << p.y << ")\n";
}
return os;
}
double ccw(const point& a, const point& b, const point& c) {
return (b.x - a.x) * (c.y - a.y) - (b.y - a.y) * (c.x - a.x);
}
double polar_angle(const point& origin, const point& p) {
return std::atan2(p.y - origin.y, p.x - origin.x);
}
std::vector<point> graham_scan(std::vector<point>& points) {
// selecting lowest point as pivot
size_t low_index = 0;
for (size_t i = 1; i < points.size(); i++) {
if (points[i].y < points[low_index].y) {
low_index = i;
}
}
std::swap(points[0], points[low_index]);
point pivot = points[0];
// sorting points by polar angle
std::sort(
points.begin() + 1,
points.end(),
[&pivot](const point& pa, const point& pb) {
return polar_angle(pivot, pa) < polar_angle(pivot, pb);
});
// creating convex hull
size_t m = 1;
for (size_t i = 2; i < points.size(); i++) {
while (ccw(points[m - 1], points[m], points[i]) <= 0) {
if (m > 1) {
m--;
continue;
} else if (i == points.size()) {
break;
} else {
i++;
}
}
m++;
std::swap(points[i], points[m]);
}
return std::vector<point>(points.begin(), points.begin() + m + 1);
}
int main() {
std::vector<point> points = {{-5, 2},
{5, 7},
{-6, -12},
{-14, -14},
{9, 9},
{-1, -1},
{-10, 11},
{-6, 15},
{-6, -8},
{15, -9},
{7, -7},
{-2, -9},
{6, -5},
{0, 14},
{2, 8}};
std::cout << "original points are as follows:\n" << points;
const std::vector<point> hull = graham_scan(points);
std::cout << "points in hull are as follows:\n" << hull;
return 0;
}