求多边形面积(如HDU2036)

来源:互联网 发布:低碳钢的弹性模量算法 编辑:程序博客网 时间:2024/05/20 21:22

1,。


2:可将多边形分割成多个三角形(如果不是按逆时针或顺时针给定顺序的定点需要进行排序),利用海伦公式,求得面积,但不适用于凹多边形,例如 HDU 2036.

海伦公式如下:已知三角形各边a,b,c  

S=sqrt(p(p-a)(p-b)(p-c))  其中p为半周长:p=(a+b+c)/2;

3:



HDU 2036 :

#include<iostream>#include<cstdlib>#include<cstdio>#include<cmath>#include<cstring>#include<string>#include<cstdlib>#include<iomanip>#include<algorithm>typedef long long LL;using namespace std;typedef struct point{    int x,y;}point;point a[100];double area(point p,point q){    return p.x * q.y - q.x * p.y;}int main(){    int i,n;    double sum;    while(scanf("%d ",&n) != EOF && n)    {        for(i=0;i<n;i++)            scanf("%d %d",&a[i].x,&a[i].y);            sum = area(a[n-1],a[0]);            for(i=1;i<n;i++)                sum +=area(a[i-1] ,a[i]);            printf("%.1f\n",0.5 * sum);    }    return 0;}


1 0