Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- struct point{
- int x, y;
- };
- point P[mx], C[mx], P0;
- int triArea(point a, point b, point c) {
- return (a.x*(b.y-c.y) + b.x*(c.y-a.y) + c.x*(a.y-b.y));
- }
- int sqDist(point a, point b) {
- return ((a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y));
- }
- int cross(point a, point b, point c) {
- return ((b.x-a.x)*(c.x-b.x) + (b.y-a.y)*(c.y-b.y));
- }
- bool comp(point a, point b) {
- int d = triArea(P0, a, b);
- if(d < 0) return false;
- if(!d && sqDist(P0, a) > sqDist(P0, b)) return false;
- return true;
- }
- inline bool normal(point a, point b) {
- return ((a.x==b.x) ? a.y < b.y : a.x < b.x);
- }
- bool issame(point a, point b) {
- return (a.x == b.x && a.y == b.y);
- }
- int makeUnique(int np) {
- sort(&P[0], &P[np], normal);
- np = unique(&P[0], &P[np], issame) - P;
- return np;
- }
- int convexHull(int np) {
- if(np<=2) {
- for(int i=0;i<np;i++){
- C[i] = P[i];
- }
- return np;
- }
- int i, j, nc, pos = 0;
- for(i = 1; i < np; i++)
- if(P[i].y<P[pos].y || (P[i].y==P[pos].y && P[i].x<P[pos].x))
- pos = i;
- swap(P[0], P[pos]);
- P0 = P[0];
- sort(&P[1], &P[np], comp);
- for(i = 0; i < 3; i++) C[i] = P[i];
- for(i = j = 3; i < np; i++) {
- while(triArea(C[j-2], C[j-1], P[i]) < 0) j--;
- C[j++] = P[i];
- }
- return nc = j;
- }
- int compress(int nc) {
- int i, j, d;
- C[nc] = C[0];
- for(i=j=1; i < nc; i++) {
- d = triArea2(C[j-1], C[i], C[i+1]);
- if(d || (!d && issame(C[j-1], C[i+1]))) C[j++] = C[i];
- }
- return nc = j;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement