码迷,mamicode.com
首页 > 移动开发 > 详细

UVA–10652 Board Wrapping[凸包]

时间:2017-10-03 17:26:25      阅读:276      评论:0      收藏:0      [点我收藏+]

标签:line   pac   cto   ret   ref   .com   back   splay   --   

技术分享

题意:

给出n个矩形的中点、长、宽和顺时针的角度。让你用最小的凸多边形把他们包起来,计算矩形面积占凸多边形的百分比。

用大白书给出的凸包的算法,将矩形的每个顶点都做一次凸包,求出凸包的面积。

#include "bits/stdc++.h"
using namespace std;
const int maxn = 3000;
const double eps=1e-8;
const double PI = acos(-1.0);
struct Point {
    double x, y;
    Point(double x = 0.0, double y = 0.0):x(x), y(y) {}
};
typedef Point Vector;
Point operator + (Point A, Point B) {
    return Point(A.x+B.x, A.y+B.y);
}
Point operator - (Point A, Point B) {
    return Point(A.x-B.x, A.y-B.y);
}
Point operator * (Point A, double p) {
    return Point(A.x*p, A.y*p);
}
Point operator / (Point A, double p) {
    return Point(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;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;
}
double Dot(Point A, Point B) {
    return A.x*B.x+A.y*B.y;
}
double Cross(Point A, Point B) {
    return A.x*B.y - A.y*B.x;
}
double Length(Point A) {return sqrt(Dot(A,A));}
Vector Normal(Vector A) {return Vector(-A.y, A.x)/Length(A);}
double Angle(Vector A, Vector B) {
    return acos(Dot(A,B)/Length(A)/Length(B));
} 
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 PolygonArea(Point* p, int n) {
    double area = 0.0;
    for (int i = 1; i < n-1; i++) 
        area += Cross(p[i]-p[0],p[i+1]-p[0]);
    return area/2.0;
}
Point p[maxn], ch[maxn];
bool cmp(Point a, Point b) {
    if (dcmp(a.x - b.x) == 0) return dcmp(a.y - a.y) <= 0;
    return dcmp(a.x - b.x) < 0;
}
//输出n个点,输出ch作为凸包所含的点
//如果不希望边上有点 将<=改为<   输入无重复点
int ConvexHull(Point* p, int n, Point* ch) {
    sort(p, p + n); //先比较x,在比较y字典序
    int m = 0;
    for (int i = 0; i < n; i++) {
        while (m > 1 && Cross(ch[m-1]-ch[m-2], p[i]-ch[m-2]) <= 0) m--;
        ch[m++] = p[i];
    }
    int k = m; 
    for (int i = n - 2; i >= 0; i--) {
        while (m > k && Cross(ch[m-1]-ch[m-2], p[i]-ch[m-2]) <= 0) m--; 
        ch[m++] = p[i];
    }
    if (n > 1) m--;
    return m;
}
int main(int argc, char const *argv[])
{
    int T;
    scanf("%d", &T);
    while (T--) {
        int N, cnt = 0;
        scanf("%d", &N);
        double ara = 0.0;
        for (int  i = 0; i < N; i++) {
            double x, y, w, h, j;
            scanf("%lf%lf%lf%lf%lf", &x, &y, &w, &h, &j);
            double ang = -(j/180.0)*PI;
            Point o = Point(x, y);
            p[cnt++] = o + Rotate(Vector(w/2, h/2), ang);
            p[cnt++] = o + Rotate(Vector(-w/2, h/2), ang);
            p[cnt++] = o + Rotate(Vector(w/2, -h/2), ang);
            p[cnt++] = o + Rotate(Vector(-w/2, -h/2), ang);
            ara += w*h;
        }
        int m = ConvexHull(p, cnt, ch);
        double A = PolygonArea(ch, m);
        printf("%.1lf %%\n", ara*100/A);
    }
    return 0;
}

UVA–10652 Board Wrapping[凸包]

标签:line   pac   cto   ret   ref   .com   back   splay   --   

原文地址:http://www.cnblogs.com/cniwoq/p/7623970.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!