|
| 1 | + |
| 2 | +#include <iostream> |
| 3 | +#include <stack> |
| 4 | +#include <stdlib.h> |
| 5 | +using namespace std; |
| 6 | + |
| 7 | +struct Point |
| 8 | +{ |
| 9 | + int x, y; |
| 10 | +}; |
| 11 | + |
| 12 | +// A globle point needed for sorting points with reference |
| 13 | +// to the first point Used in compare function of qsort() |
| 14 | +Point p0; |
| 15 | + |
| 16 | +// A utility function to find next to top in a stack |
| 17 | +Point nextToTop(stack<Point> &S) |
| 18 | +{ |
| 19 | + Point p = S.top(); |
| 20 | + S.pop(); |
| 21 | + Point res = S.top(); |
| 22 | + S.push(p); |
| 23 | + return res; |
| 24 | +} |
| 25 | + |
| 26 | +// A utility function to swap two points |
| 27 | +int swap(Point &p1, Point &p2) |
| 28 | +{ |
| 29 | + Point temp = p1; |
| 30 | + p1 = p2; |
| 31 | + p2 = temp; |
| 32 | +} |
| 33 | + |
| 34 | +// A utility function to return square of distance |
| 35 | +// between p1 and p2 |
| 36 | +int distSq(Point p1, Point p2) |
| 37 | +{ |
| 38 | + return (p1.x - p2.x)*(p1.x - p2.x) + |
| 39 | + (p1.y - p2.y)*(p1.y - p2.y); |
| 40 | +} |
| 41 | + |
| 42 | +// To find orientation of ordered triplet (p, q, r). |
| 43 | +// The function returns following values |
| 44 | +// 0 --> p, q and r are colinear |
| 45 | +// 1 --> Clockwise |
| 46 | +// 2 --> Counterclockwise |
| 47 | +int orientation(Point p, Point q, Point r) |
| 48 | +{ |
| 49 | + int val = (q.y - p.y) * (r.x - q.x) - |
| 50 | + (q.x - p.x) * (r.y - q.y); |
| 51 | + |
| 52 | + if (val == 0) return 0; // colinear |
| 53 | + return (val > 0)? 1: 2; // clock or counterclock wise |
| 54 | +} |
| 55 | + |
| 56 | +// A function used by library function qsort() to sort an array of |
| 57 | +// points with respect to the first point |
| 58 | +int compare(const void *vp1, const void *vp2) |
| 59 | +{ |
| 60 | + Point *p1 = (Point *)vp1; |
| 61 | + Point *p2 = (Point *)vp2; |
| 62 | + |
| 63 | + // Find orientation |
| 64 | + int o = orientation(p0, *p1, *p2); |
| 65 | + if (o == 0) |
| 66 | + return (distSq(p0, *p2) >= distSq(p0, *p1))? -1 : 1; |
| 67 | + |
| 68 | + return (o == 2)? -1: 1; |
| 69 | +} |
| 70 | + |
| 71 | +// Prints convex hull of a set of n points. |
| 72 | +void convexHull(Point points[], int n) |
| 73 | +{ |
| 74 | + // Find the bottommost point |
| 75 | + int ymin = points[0].y, min = 0; |
| 76 | + for (int i = 1; i < n; i++) |
| 77 | + { |
| 78 | + int y = points[i].y; |
| 79 | + |
| 80 | + // Pick the bottom-most or chose the left |
| 81 | + // most point in case of tie |
| 82 | + if ((y < ymin) || (ymin == y && |
| 83 | + points[i].x < points[min].x)) |
| 84 | + ymin = points[i].y, min = i; |
| 85 | + } |
| 86 | + |
| 87 | + // Place the bottom-most point at first position |
| 88 | + swap(points[0], points[min]); |
| 89 | + |
| 90 | + // Sort n-1 points with respect to the first point. |
| 91 | + // A point p1 comes before p2 in sorted ouput if p2 |
| 92 | + // has larger polar angle (in counterclockwise |
| 93 | + // direction) than p1 |
| 94 | + p0 = points[0]; |
| 95 | + qsort(&points[1], n-1, sizeof(Point), compare); |
| 96 | + |
| 97 | + // If two or more points make same angle with p0, |
| 98 | + // Remove all but the one that is farthest from p0 |
| 99 | + // Remember that, in above sorting, our criteria was |
| 100 | + // to keep the farthest point at the end when more than |
| 101 | + // one points have same angle. |
| 102 | + int m = 1; // Initialize size of modified array |
| 103 | + for (int i=1; i<n; i++) |
| 104 | + { |
| 105 | + // Keep removing i while angle of i and i+1 is same |
| 106 | + // with respect to p0 |
| 107 | + while (i < n-1 && orientation(p0, points[i], |
| 108 | + points[i+1]) == 0) |
| 109 | + i++; |
| 110 | + |
| 111 | + |
| 112 | + points[m] = points[i]; |
| 113 | + m++; // Update size of modified array |
| 114 | + } |
| 115 | + |
| 116 | + // If modified array of points has less than 3 points, |
| 117 | + // convex hull is not possible |
| 118 | + if (m < 3) return; |
| 119 | + |
| 120 | + // Create an empty stack and push first three points |
| 121 | + // to it. |
| 122 | + stack<Point> S; |
| 123 | + S.push(points[0]); |
| 124 | + S.push(points[1]); |
| 125 | + S.push(points[2]); |
| 126 | + |
| 127 | + // Process remaining n-3 points |
| 128 | + for (int i = 3; i < m; i++) |
| 129 | + { |
| 130 | + // Keep removing top while the angle formed by |
| 131 | + // points next-to-top, top, and points[i] makes |
| 132 | + // a non-left turn |
| 133 | + while (orientation(nextToTop(S), S.top(), points[i]) != 2) |
| 134 | + S.pop(); |
| 135 | + S.push(points[i]); |
| 136 | + } |
| 137 | + |
| 138 | + // Now stack has the output points, print contents of stack |
| 139 | + while (!S.empty()) |
| 140 | + { |
| 141 | + Point p = S.top(); |
| 142 | + cout << "(" << p.x << ", " << p.y <<")" << endl; |
| 143 | + S.pop(); |
| 144 | + } |
| 145 | +} |
| 146 | + |
| 147 | +int main() |
| 148 | +{ |
| 149 | + Point points[] = {{0, 3}, {1, 1}, {2, 2}, {4, 4}, |
| 150 | + {0, 0}, {1, 2}, {3, 1}, {3, 3}}; |
| 151 | + int n = sizeof(points)/sizeof(points[0]); |
| 152 | + convexHull(points, n); |
| 153 | + return 0; |
| 154 | +} |
| 155 | + |
| 156 | +/* |
| 157 | +(0, 3) |
| 158 | +(4, 4) |
| 159 | +(3, 1) |
| 160 | +(0, 0) |
| 161 | +*/ |
0 commit comments