UVa 10256 凸包简单应用

题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1593

#include<cstdio>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
const int maxn = 606;
const int maxe = 100000;
const int INF = 0x3f3f3f;
const double eps = 1e-8;
const double PI = acos(-1.0);

struct Point{
    double x,y;
    Point(double x=0, double y=0) : x(x),y(y){ }    //构造函数
};
typedef Point Vector;

Vector operator + (Vector A , Vector B){return Vector(A.x+B.x,A.y+B.y);}
Vector operator - (Vector A , Vector B){return Vector(A.x-B.x,A.y-B.y);}
Vector operator * (Vector A , double p){return Vector(A.x*p,A.y*p);}
Vector operator / (Vector A , double p){return Vector(A.x/p,A.y/p);}

bool operator < (const Point& a,const Point& b){
    return a.x < b.x ||( a.x == b.x && a.y < b.y);
}

int dcmp(double x){
    if(fabs(x) < eps) return 0;
    else              return x < 0 ? -1 : 1;
}
bool operator == (const Point& a, const Point& b){
    return dcmp(a.x - b.x) == 0 && dcmp(a.y - b.y) == 0;
}

///向量(x,y)的极角用atan2(y,x);
double Dot(Vector A, Vector B){ return A.x*B.x + A.y*B.y; }
double Length(Vector A)    { return sqrt(Dot(A,A)); }
double Angle(Vector A, Vector B)  { return acos(Dot(A,B) / Length(A) / Length(B)); }

double Cross(Vector A, Vector B)  { return A.x*B.y - A.y * B.x; }

Vector Rotate(Vector A, double rad) { return Vector(A.x*cos(rad)-A.y*sin(rad),A.x*sin(rad)+A.y*cos(rad)); }
double torad(double deg) { return deg/180 * PI; }

double PolygonArea(Point* p,int n){  //n代表定点数;
    double area = 0;
    for(int i=1;i<n-1;i++){
        area += Cross(p[i]-p[0],p[i+1]-p[0]);
    }
    return area/2;
}

//凸包:
/**Andrew算法思路:首先按照先x后y从小到大排序(这个地方没有采用极角逆序排序,所以要进行两次扫描),删除重复的点后得到的序列p1,p2.....,然后把p1和p2放到凸包中。从p3开始,当新的
点在凸包“前进”方向的左边时继续,否则依次删除最近加入凸包的点,直到新点在左边;**/

//Goal[]数组模拟栈的使用;
int ConvexHull(Point* P,int n,Point* Goal){
    sort(P,P+n);
    int m = unique(P,P+n) - P;    //对点进行去重;
    int cnt = 0;
    for(int i=0;i<m;i++){       //求下凸包;
        while(cnt>1 && dcmp(Cross(Goal[cnt-1]-Goal[cnt-2],P[i]-Goal[cnt-2])) <= 0)  cnt--;
        Goal[cnt++] = P[i];
    }
    int temp = cnt;
    for(int i=m-2;i>=0;i--){     //逆序求上凸包;
        while(cnt>temp && dcmp(Cross(Goal[cnt-1]-Goal[cnt-2],P[i]-Goal[cnt-2])) <= 0) cnt--;
        Goal[cnt++] = P[i];
    }
    if(cnt > 1) cnt--;
    return cnt;
}
/*********************************分割线******************************/

Point P[maxn*4],Goal[maxn*4];
int n;
double area1,area2;

int main()
{
    //freopen("E:\acm\input.txt","r",stdin);
    int T;
    cin>>T;
    while(T--){
        cin>>n;
        area1 = 0;
        int cnt = 0;
        double x1,y1,w,h,angle;
        double a1,a2,b1,b2;
        for(int i=0;i<n;i++){
            scanf("%lf %lf %lf %lf %lf",&x1,&y1,&w,&h,&angle);
            Point Cen(x1,y1);
            angle = -torad(angle);
            P[cnt++] = Cen + Rotate(Vector(w/2,h/2),angle);
            P[cnt++] = Cen + Rotate(Vector(w/2,-h/2),angle);
            P[cnt++] = Cen + Rotate(Vector(-w/2,h/2),angle);
            P[cnt++] = Cen + Rotate(Vector(-w/2,-h/2),angle);
            area1 += w * h;
        }
        cnt = ConvexHull(P,cnt,Goal);
        area2 = PolygonArea(Goal,cnt);
        printf("%.1f %%
",area1*100/area2);
    }
    return 0;
}
View Code
原文地址:https://www.cnblogs.com/acmdeweilai/p/3253129.html