Poj3348 Cows

有一块多边形样的土地,每头牛的生存需要50平方米,问这块地能养活多少牛。
凸包面积。

#include
#include
#include
#include
#include
#include
using namespace std;
const int maxn = 10005;
#define INF 1e9
#define EPS 1e-6
struct point {
    double x,y;
    point(){}
    point(double _x,double _y):x(_x),y(_y){}
    point operator - (const point &a) {return point(x-a.x,y-a.y);}
};
double cross(point a,point b) {return a.x*b.y-a.y*b.x;}
double dis(point a,point b) {point c=a-b; return sqrt(1.0*c.x*c.x+1.0*c.y*c.y);}
bool cmp(point a,point b) {return b.x-a.x > EPS || (fabs(a.x-b.x) EPS);}
point P[maxn],hull[maxn];
int cnt,N;
double s[maxn];
void quickhull(int L,int R,point a,point b) {
    int x=L,i=L-1,j=R+1;
    for(int k = L; k <= R; k++) if( s[k]-s[x] > EPS || (fabs(s[k]-s[x])for(int k = L; k <= R; k++) {
        s[++i] = cross(a-P[k],y-P[k]);
        if( s[i] > EPS) swap(P[i],P[k]);else i--;
    }
    for(int k = R; k >= L; k--) {
        s[--j] = cross(y-P[k],b-P[k]);
        if( s[j] > EPS) swap(P[j],P[k]);else j++;
    }
    if( L <= i )quickhull(L,i,a,y);
    hull[++cnt] = y;
    if( j <= R )quickhull(j,R,y,b);
}
int main() {
    while(~scanf("%d",&N)){
        memset(s,0,sizeof s);
        P[0] = point(INF,INF);
        int x = 0;
        for(int i = 1; i <= N; i++) {
            scanf("%lf%lf",&P[i].x,&P[i].y);
            if( cmp(P[i],P[x]) ) x=i;
        }
        swap(P[x],P[1]);
        cnt=0;
        hull[++cnt] = P[1];
        quickhull(2,N,P[1],P[1]);

        point a = hull[1];
        point b = hull[2];
        point c;
        double area=0;

        for(int i = 3; i <=cnt; i++) {
            c = hull[i];
            area += fabs(cross(b-a,c-a)/2.0);
            b=c;
        }

        int ans =(int) area/50.0;
        ans = abs(ans);
        printf("%d\n",ans);
    }
}

你可能感兴趣的:(计算几何)