-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconvex_hull.cpp
More file actions
183 lines (160 loc) · 5.19 KB
/
convex_hull.cpp
File metadata and controls
183 lines (160 loc) · 5.19 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
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
#include <algorithm>
#include <iostream>
#include <optional>
template <typename T>
struct Point {
T x;
T y;
};
/**
* @brief Get the Cross Product of segment (a, b) and (a, c)
*
* Example:
* W(3, 4)
* V(5, 2)
* (0,0)
*
* V x W = VxWy - WxVy
* = | 5 3 |
* | 2 4 |
* = (5 * 4) - (2 * 3) = 14 (counter-clockwise)
*
* @tparam T
* @param a
* @param b
* @param c
* @return T positive for counter-clockwise, negative for clockwise, zero for
* collinear
*/
template <typename T>
T GetCrossProduct(const Point<T> &a, const Point<T> &b, const Point<T> &c) {
return ((b.x - a.x) * (c.y - a.y) - (b.y - a.y) * (c.x - a.x));
}
template <typename T>
Point<T> SwapAndGetFirstPoint(std::vector<Point<T>> &points) {
auto iterator = std::min_element(
points.begin(), points.end(), [](const Point<T> a, const Point<T> b) {
return ((a.y < b.y) || ((a.y == b.y) && (a.x < b.x)));
});
auto old_first_point = points[0];
points[0] = *iterator;
*iterator = old_first_point;
return points[0];
}
template <typename T>
std::vector<Point<T>> GetConvexHull(std::vector<Point<T>> &points) {
if (points.size() < 3) {
return std::vector<Point<T>>{};
}
auto first_point = SwapAndGetFirstPoint(points);
std::sort(points.begin() + 1, points.end(),
[&](const Point<T> &b, const Point<T> &c) {
return (GetCrossProduct(first_point, b, c) < 0);
});
std::vector<Point<T>> result;
auto it = points.begin();
auto value = *it++;
std::cout << "Adding... " << value.x << " " << value.y << std::endl;
result.push_back(value);
value = *it++;
std::cout << "Adding... " << value.x << " " << value.y << std::endl;
result.push_back(value);
value = *it++;
std::cout << "Adding... " << value.x << " " << value.y << std::endl;
result.push_back(value);
while (it != points.end()) {
// StepConverge
// Pop off any points that make a convex angle with *it
while (GetCrossProduct(*(result.rbegin() + 1), *(result.rbegin()), *it) >=
0) {
std::cout << "Popping out: " << result.back().x << " " << result.back().y
<< std::endl;
result.pop_back();
}
auto value = *it++;
std::cout << "Adding... " << value.x << " " << value.y << std::endl;
result.push_back(value);
}
return result;
}
template <typename T>
class ConvexHull {
public:
using Callback = std::function<void(const Point<T> &)>;
explicit ConvexHull(const std::vector<Point<T>> &points)
: points_(points), current_index_(0) {
auto first_point = SwapAndGetFirstPoint(points_);
std::sort(points_.begin() + 1, points_.end(),
[&](const Point<T> &b, const Point<T> &c) {
return (GetCrossProduct(first_point, b, c) < 0);
});
initialize_first_three_points_ = true;
}
std::optional<std::vector<Point<T>>> StepForward(Callback insert_callback,
Callback pop_callback) {
if (points_.size() < 3) {
return std::nullopt;
} else {
if (initialize_first_three_points_) {
for (auto i = 0; i < 3; ++i) {
InsertCurrentValueWithCallback(insert_callback);
}
initialize_first_three_points_ = false;
return std::nullopt;
}
}
if (current_index_ < points_.size()) {
if (GetCrossProduct(*(results_.rbegin() + 1), *(results_.rbegin()),
points_.at(current_index_)) >= 0) {
PopCurrentValueWithCallback(pop_callback);
} else {
InsertCurrentValueWithCallback(insert_callback);
}
} else {
return results_;
}
return std::nullopt;
}
private:
void InsertCurrentValueWithCallback(Callback insert_callback) {
const auto value = points_.at(current_index_);
insert_callback(value);
results_.push_back(value);
current_index_++;
}
void PopCurrentValueWithCallback(Callback pop_callback) {
pop_callback(results_.back());
results_.pop_back();
}
std::vector<Point<T>> points_;
std::vector<Point<T>> results_;
size_t current_index_;
bool initialize_first_three_points_;
};
int main() {
auto points = std::vector<Point<int>>{{0, 3}, {1, 1}, {2, 2}, {4, 4},
{0, 0}, {1, 2}, {3, 1}, {3, 3}};
auto result = GetConvexHull(points);
std::cout << "Results" << std::endl;
for (const auto value : result) {
std::cout << value.x << " " << value.y << std::endl;
}
std::cout << "-------------------" << std::endl;
auto convex_hull = ConvexHull(points);
while (convex_hull.StepForward(
[](const Point<int> &point) {
std::cout << "Adding... " << point.x << " " << point.y
<< std::endl;
},
[](const Point<int> &point) {
std::cout << "Popping out: " << point.x << " " << point.y
<< std::endl;
}) == std::nullopt) {
}
auto new_result = convex_hull.StepForward([](const Point<int> &) {},
[](const Point<int> &) {});
std::cout << "Results" << std::endl;
for (const auto value : *new_result) {
std::cout << value.x << " " << value.y << std::endl;
}
}