This commit is contained in:
Lajat5 2021-11-06 09:02:27 +00:00
parent 2d15e14e1e
commit 69b6832b9a

View File

@ -186,33 +186,32 @@ std::vector<Point> convexHull(std::vector<Point> points, uint64_t size) {
// If modified array of points has less than 3 points, convex hull is not // If modified array of points has less than 3 points, convex hull is not
// possible // possible
if (m < 3) { if (m < 3) {
return {} return {};
}; };
// Create an empty stack and push first three points to it. // Create an empty stack and push first three points to it.
std::stack<Point> S; std::stack<Point> St;
S.push(points[0]); St.push(points[0]);
S.push(points[1]); St.push(points[1]);
S.push(points[2]); St.push(points[2]);
// Process remaining n-3 points // Process remaining n-3 points
for (int i = 3; i < m; i++) { for (int i = 3; i < m; i++) {
// Keep removing top while the angle formed by // Keep removing top while the angle formed by
// points next-to-top, top, and points[i] makes // points next-to-top, top, and points[i] makes
// a non-left turn // a non-left turn
while (S.size() > 1 && while (St.size() > 1 && orientation(nextToTop(&St), St.top(), points[i]) != 2) {
orientation(nextToTop(S), S.top(), points[i]) != 2) { St.pop();
S.pop();
} }
S.push(points[i]); St.push(points[i]);
} }
std::vector<Point> result; std::vector<Point> result;
// Now stack has the output points, push them into the resultant vector // Now stack has the output points, push them into the resultant vector
while (!S.empty()) { while (!St.empty()) {
Point p = S.top(); Point p = St.top();
result.push_back(p); result.push_back(p);
S.pop(); St.pop();
} }
return result; // return resultant vector with Convex Hull co-ordinates. return result; // return resultant vector with Convex Hull co-ordinates.