poj2007 (polar angle sort)

Topic link: https: //vjudge.net/problem/POJ-2007

The meaning of problems: scrambled give all the vertices of the convex hull, according to the polar angle vertex order output.

Ideas: Sort by polar angle once.

AC code:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;

const int maxn=105;
const double PI=acos(-1.0);

struct Point{
    int x,y;
    Point():x(0),y(0){}
    Point(int x,int y):x(x),y(y){}
}list[maxn];
int stack[maxn],top;

//计算叉积p0p1×p0p2
int cross(Point p0,Point p1,Point p2){
    return (p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y);
}
// calculate the distance p1p2 
Double DIS (Point P1, P2 Point) {
     return sqrt (( Double ) (P2.x-P1.x) * (P2.x-P1.x) + (p2.y-p1.y ) * (p2.y- p1.y));
}
// polar angle sorting function, the same as the angular distance in front of the small 
BOOL CMP (Point P1, P2 Point) {
     int tmp = Cross (List [ 0 ], P1, P2);
     IF (tmp> 0 ) return  to true ;
     the else  IF (tmp == 0 && DIS (List [ 0 ], P1) <DIS (List [ 0 ], P2)) return  to true ;
     the else  return  to false ;
}

int main(){
    int x,y,n=0;
    while(~scanf("%d%d",&x,&y)){
        list[n].x=x,list[n].y=y;
        ++n;
    }
    sort(list+1,list+n,cmp);
    for(int i=0;i<n;++i)
        printf("(%d,%d)\n",list[i].x,list[i].y);
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/FrankChen831X/p/11824995.html