-
Notifications
You must be signed in to change notification settings - Fork 0
/
ConvexHull.h
113 lines (97 loc) · 2.91 KB
/
ConvexHull.h
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
// Most of this code is by Amritya Yagni, as posted at
// https://www.geeksforgeeks.org/quickhull-algorithm-convex-hull/
// This code just lacks the driver.
#include <cmath>
#include <set>
#include <utility>
#include <iostream>
using namespace std;
#define iPair pair<float, float>
// Stores the result (points of convex hull)
set<iPair> hull;
// Returns the side of point p with respect to line
// joining points p1 and p2.
int findSide(iPair p1, iPair p2, iPair p)
{
float val = (p.second - p1.second) * (p2.first - p1.first) -
(p2.second - p1.second) * (p.first - p1.first);
if (val > 0)
return 1;
if (val < 0)
return -1;
return 0;
}
// returns a value proportional to the distance
// between the point p and the line joining the
// points p1 and p2
float lineDist(iPair p1, iPair p2, iPair p)
{
return abs ((p.second - p1.second) * (p2.first - p1.first) -
(p2.second - p1.second) * (p.first - p1.first));
}
// End points of line L are p1 and p2. side can have value
// 1 or -1 specifying each of the parts made by the line L
void quickHull(iPair a[], int n, iPair p1, iPair p2, int side)
{
int ind = -1;
float max_dist = 0;
// finding the point with maximum distance
// from L and also on the specified side of L.
for (int i=0; i<n; i++)
{
float temp = lineDist(p1, p2, a[i]);
if (findSide(p1, p2, a[i]) == side && temp > max_dist)
{
ind = i;
max_dist = temp;
}
}
// If no point is found, add the end points
// of L to the convex hull.
if (ind == -1)
{
hull.insert(p1);
hull.insert(p2);
return;
}
// Recur for the two parts divided by a[ind]
quickHull(a, n, a[ind], p1, -findSide(a[ind], p1, p2));
quickHull(a, n, a[ind], p2, -findSide(a[ind], p2, p1));
}
set<iPair> printHull(iPair a[], int n)
{
set<iPair> res;
// a[i].second -> y-coordinate of the ith point
if (n < 3)
{
cout << "Convex hull not possible\n";
return res;
}
// Finding the point with minimum and
// maximum x-coordinate
int min_x = 0, max_x = 0;
for (int i=1; i<n; i++)
{
if (a[i].first < a[min_x].first)
min_x = i;
if (a[i].first > a[max_x].first)
max_x = i;
}
// Recursively find convex hull points on
// one side of line joining a[min_x] and
// a[max_x]
quickHull(a, n, a[min_x], a[max_x], 1);
// Recursively find convex hull points on
// other side of line joining a[min_x] and
// a[max_x]
quickHull(a, n, a[min_x], a[max_x], -1);
// cout << "The points in Convex Hull are:\n";
while (!hull.empty())
{
res.insert(*(hull.begin()));
// cout << "(" <<( *hull.begin()).first << ", "
// << (*hull.begin()).second << ") ";
hull.erase(hull.begin());
}
return res;
}