[poj] 3907 Build Your Home || 求多边形面积

原题

多组数据,到0为止。
每次给出按顺序的n个点(可能逆时针,可能顺时针),求多边形面积(保留整数)


多边形面积为依次每条边(向量)叉积/2的和
(S=sum _{i=1}^{n-1}p[i]*p[i+1]/2)
//逆时针为正,顺时针为负

#include<cstdio>
#define N 1010
using namespace std;
int n;
struct hhh
{
    double x,y;
    hhh() {}
    hhh(double _x,double _y) : x(_x),y(_y) {}
    double operator * (const hhh &b) const
	{
	    return x*b.y-b.x*y;
	}
}p[N];

double Area()
{
    double ans=0;
    for (int i=2;i<=n;i++)
	ans+=(p[i-1]*p[i])/2;
    ans+=(p[n]*p[1])/2;
    return ans;
}

double abs(double x) { return x>=0?x:-x; }

int main()
{
    while (~scanf("%d",&n) && n)
    {
	for (int i=1;i<=n;i++)
	    scanf("%lf%lf",&p[i].x,&p[i].y);
	printf("%.f
",abs(Area()));
    }
    return 0;
}
原文地址:https://www.cnblogs.com/mrha/p/8066976.html