UVA–10652 Board Wrapping[凸包]

來源:互聯網
上載者:User

標籤: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[凸包]

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.