This repository has been archived by the owner on Feb 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Randomized_Incremental_Construction.cpp
61 lines (61 loc) · 1.93 KB
/
Randomized_Incremental_Construction.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
#include <iostream>
#include <vector>
#include <algorithm>
struct Point {
int x, y;
Point(int _x, int _y) : x(_x), y(_y) {}
bool operator<(const Point& other) const {
return x < other.x || (x == other.x && y < other.y);
}
};
int orientation(const Point& p, const Point& q, const Point& r) {
int val = (q.y - p.y) * (r.x - q.x) - (q.x - p.x) * (r.y - q.y);
if (val == 0) return 0;
return (val > 0) ? 1 : 2;
}
std::vector<Point> convexHullRIC(std::vector<Point>& points) {
std::vector<Point> hull;
std::sort(points.begin(), points.end());
hull.push_back(points[0]);
hull.push_back(points.back());
std::vector<Point> upSet, downSet;
for (size_t i = 1; i < points.size() - 1; ++i) {
int orientationVal = orientation(points[0], points.back(), points[i]);
if (orientationVal == 2) {
upSet.push_back(points[i]);
} else if (orientationVal == 1) {
downSet.push_back(points[i]);
}
}
for (const Point& p : upSet) {
while (hull.size() >= 2 && orientation(hull[hull.size() - 2], hull.back(), p) != 2) {
hull.pop_back();
}
hull.push_back(p);
}
for (const Point& p : downSet) {
while (hull.size() >= 2 && orientation(hull[hull.size() - 2], hull.back(), p) != 2) {
hull.pop_back();
}
hull.push_back(p);
}
return hull;
}
int main() {
int n;
std::cout << "Enter the number of points: ";
std::cin >> n;
std::vector<Point> points;
std::cout << "Enter the coordinates of each point (x y):" << std::endl;
for (int i = 0; i < n; ++i) {
int x, y;
std::cin >> x >> y;
points.emplace_back(x, y);
}
std::vector<Point> convexHull = convexHullRIC(points);
std::cout << "Convex Hull Points:" << std::endl;
for (const Point& p : convexHull) {
std::cout << "(" << p.x << ", " << p.y << ")" << std::endl;
}
return 0;
}