Uva - 10652 Board Wrapping(凸包)

來源:互聯網
上載者:User

標籤:

給定一些長方形的座標,求長方形的面積與圍住這些長方形面積的凸包的百分比.

首先假設長方形都是水平放置,那麼根據長和寬還有中心座標,可以求出四個頂點的座標,然後求出從中心指向頂點的向量,和角度轉化成的弧度,向量旋轉之後得到一個新的向量

是由中心指向新的頂點,加上中心點就得到新的頂點的座標.可以畫圖理解.

 1 #include <cstdio> 2 #include <cstring> 3 #include <cmath> 4 #include <algorithm> 5 using namespace std; 6 const double PI = acos(-1.0); 7 const int maxn=2500; 8 double torad(double deg) {return deg/180*PI;} //角度轉化成弧度 9 10 struct Point11 {12     double x,y;13     Point(double x=0,double y=0):x(x),y(y) {}14 }p[maxn],ch[maxn];15 typedef Point Vector;16 17 Vector operator + (const Vector& A, const Vector& B) { return Vector(A.x+B.x, A.y+B.y); }18 Vector operator - (const Point& A, const Point& B) { return Vector(A.x-B.x, A.y-B.y); }19 Vector operator * (const Vector& A, double p) { return Vector(A.x*p, A.y*p); }20 Vector operator / (const Vector& A, double p) { return Vector(A.x/p, A.y/p); }21 22 bool operator < (const Point& a, const Point& b)    //結構體運算子的重載23 {24   return a.x < b.x || (a.x == b.x && a.y < b.y);25 }26 27 const double eps = 1e-8;  //比較函數28 int dcmp(double x) { if(fabs(x) < eps) return 0; else return x < 0 ? -1 : 1; }29 30 bool operator == (const Point& a, const Point &b)31 {32   return dcmp(a.x-b.x) == 0 && dcmp(a.y-b.y) == 0;33 }34 35 //基本運算:36 double dist(const Vector& A, const Vector& B) {return sqrt(pow(A.x-B.x,2)+pow(A.y-B.y,2));}37 double Dot(const Vector& A, const Vector& B) { return A.x*B.x + A.y*B.y; }38 double Length(const Vector& A) { return sqrt(Dot(A, A)); }39 double Angle(const Vector& A, const Vector& B) { return acos(Dot(A, B) / Length(A) / Length(B)); }40 double Cross(const Vector& A, const Vector& B) { return A.x*B.y - A.y*B.x; }41 double Area2(Point A, Point B, Point C) {return Cross(B-A, C-A);}42 43 //向量旋轉 rad是弧度  逆時針為正 44 Vector Rotate(const Vector& A, double rad)45 {46   return Vector(A.x*cos(rad)-A.y*sin(rad), A.x*sin(rad)+A.y*cos(rad));47 }48 49 int main()50 {51     //freopen("a.txt","r",stdin);52     int t,N,n;53     double a,b,c,d,e;54     Point s,s1;55     scanf("%d",&t);56     while(t--)57     {58         scanf("%d",&N);59         n=0;60         double area=0,sum=0;61         for(int i=0;i<N;i++)62         {63             scanf("%lf%lf%lf%lf%lf",&s.x,&s.y,&c,&d,&e);64             sum+=c*d;65             e=torad(e);  //轉化成弧度66             s1=Point(s.x-c/2,s.y-d/2); //得到 四個角的座標67             p[n++]=s+Rotate(s1-s,-e);  //必須是向量旋轉,得到頂點指向中心點的向量,然後旋轉,加上 s 得到旋轉後的頂點座標68             s1=Point(s.x-c/2,s.y+d/2);69             p[n++]=s+Rotate(s1-s,-e);70             s1=Point(s.x+c/2,s.y+d/2);71             p[n++]=s+Rotate(s1-s,-e);72             s1=Point(s.x+c/2,s.y-d/2);73             p[n++]=s+Rotate(s1-s,-e);74         }75         sort(p,p+n); //排序 後  求凸包76         int m=0;77         for(int i=0;i<n;i++)78         {79             while(m>1&&dcmp(Cross(ch[m-1]-ch[m-2],p[i]-ch[m-2]))<=0) m--;80             ch[m++]=p[i];81         }82         int k=m;83         for(int i=n-2;i>=0;i--)84         {85             while(m>k&&dcmp(Cross(ch[m-1]-ch[m-2],p[i]-ch[m-2]))<=0) m--;86             ch[m++]=p[i];87         }88         for(int i=1;i<m-1;i++)  //求凸包面積  (這裡是兩倍)89         {90             area+=Cross(ch[i]-ch[0],ch[i+1]-ch[0]);91         }92         area/=2;93         area=sum/area*100; //得到 木板占凸包的 百分比94         printf("%.1lf %%\n",area);95     }96     return 0;97 }

 

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.