poj 3907 Build Your Home(任意多边形面积)

题目:http://poj.org/problem?id=3907

大意:给出数个点,求出它们构成的多边形的面积。

运用叉积求解面积,有关叉积的性质:c=a×b = x1*y2 - y1*x2,其结果是一个矢量。c垂直于a,b所在的平面。|c|=|a||b||sin<a,b>|   它的1/2就是三角形的面积。
若 a × b > 0 , 则a在b的顺时针方向  (当然,这因具体的函数不同而不同)。 
若 a × b < 0 , 则a在b的逆时针方向。 
若 a × b = 0 , 则a与b共线,但可能同向也可能反向。
对于多边形的面积:s=0;  loop i=1-->n-2: s+=cross(p[0],p[i],[i+1]);   s=[fabs(s)+1]/2  (精度处理了)。


#include <iostream>
#include<cstdio>
#include<cmath>
using namespace std;
const int maxn=1e3+10;
typedef struct Point{
    double x,y;
}point;
point p[maxn];
double cross(point b,point c){
    return (c.x-p[0].x)*(b.y-p[0].y)-(c.y-p[0].y)*(b.x-p[0].x);
}
int main()
{
    //freopen("cin.txt","r",stdin);
    int k;
    while(cin>>k&&k){
        for(int i=0;i<k;i++){
             scanf("%lf%lf",&p[i].x,&p[i].y);
        }
        double ans=0;
        for(int i=1;i<k-1;i++){
             ans+=cross(p[i],p[i+1]);
        }
        ans=fabs(ans);
        int res=int(ans+1)/2;
        printf("%d\n",res);
    }
    return 0;
}


你可能感兴趣的:(数学,poj)