HDU 2036 計算多邊形面積

來源:互聯網
上載者:User

計算多邊形面積,這個題目比較簡單,練習一下多邊形面積的求法

就是從第一個頂點開始,順序串連順序的兩個頂點求三角形面積

這裡有個問題,如果是凸多邊形,一看就知道這個演算法是正確的

但是如果不是凸多邊形呢,其實這個結論還是正確的,因為計算

多出來的負的面積,和計算多出來的正的面積會正好抵消,這個

結論話畫個圖示範一下就OK了!

#include <iostream>#include <stdio.h>#include <string.h>#include <algorithm>using namespace std;struct point{    double x;    double y;}po[200];double xmult(point &a,point &b,point &c){    return (b.x-a.x)*(c.y-a.y) - (c.x-a.x)*(b.y-a.y);}double cal_p_area(int n){    int i,j,k;    double ans;    for(i=1;i<n-1;i++)    {        ans+=xmult(po[0],po[i],po[i+1]);    }    return ans;}int main(){    int i,j,k,n;    while(scanf("%d",&n),n)    {        for(i=0;i<n;i++)        scanf("%lf%lf",&po[i].x,&po[i].y);        printf("%.1lf\n",cal_p_area(n)/2);    }    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.