poj 3348 - 凸包面積計算

來源:互聯網
上載者:User

解題思路:

這題就是求凸包面積,然後除以50就行了。不懂啥是凸包的可以去百度

求面積的話,選取凸包上的一個點作為基點,然後把多邊形分成好多三角形,然後用叉積去算三角形面積就可以了。


#include<math.h>#include<iostream>#include<cstring>#include<algorithm>#include<cstdio>using namespace std;typedef long long ll;const int mx = 1e5 + 10;int n,top,_x,_y;struct node{int x,y;}s[mx],tubeg[mx],w;//在向量右側值為負 int judge(node p1,node p2,node p0)//面積公式判斷正負值 {int ans = (p1.x-p0.x)*(p2.y-p0.y) - (p2.x-p0.x)*(p1.y-p0.y);return ans; }bool cmp(node a,node b){int c = judge(w,b,a);//極角排序,同角度按距離從小到大排 if(!c) return pow(a.x-w.x,2)+pow(a.y-w.y,2) < pow(b.x-w.x,2)+pow(b.y-w.y,2);return c < 0;}void Graham(){tubeg[0] = s[0],tubeg[1] = s[1],tubeg[2] = s[2];//0和1肯定是凸包上的點 top = 3;for(int i=3;i<n;i++){while(judge(tubeg[top-2],s[i],tubeg[top-1])>=0) top--;//在向量左側的點都去掉 tubeg[top++] = s[i];}}int main(){while(~scanf("%d",&n)){int a,b,p = 0;for(int i=0;i<n;i++){scanf("%d%d",&s[i].x,&s[i].y);if(s[i].y<s[p].y) p = i;else if(s[i].y==s[p].y&&s[i].x<s[p].x) p = i;}if(n<=2){puts("0.00");continue;}swap(s[0],s[p]),w.x = s[0].x,w.y = s[0].y;sort(s+1,s+n,cmp);//以最下左那個座標為參考座標做極角排序 Graham();double ans = 0,x1,x2,y1,y2; for(int i=1;i<top-1;i++){x1 = tubeg[0].x - tubeg[i].x,y1 = tubeg[0].y - tubeg[i].y;x2 = tubeg[0].x - tubeg[i+1].x,y2 = tubeg[0].y - tubeg[i+1].y;ans += fabs(x1*y2-x2*y1)/2;//向量叉積求面積 }printf("%d\n",int(ans/50));}return 0;} 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.