计算几何基础_多边形面积

题目:P1183 多边形的面积 (很裸的一道题)
ps:顶点按逆时针方向逐个给出
代码:

#include
#include
#include
#include
#define N 105
using namespace std;
struct node{
    double x,y;
}a[N];
node zero;
double cross(node p0,node p1,node p2){
    return (p1.x - p0.x)*(p2.y - p0.y)-(p2.x - p0.x)*(p1.y - p0.y);
}
int main(){
    int n;
    zero.x = 0;
    zero.y = 0;
    scanf("%d",&n);
    for(int i=0;iscanf("%lf%lf",&a[i].x,&a[i].y);
    }
    double ans = 0;
    for(int i = 0;i < n;i++){
        ans += cross(zero,a[i],a[i+1]);
        //cout<
    }
    ans+= cross(zero,a[n-1],a[0]);
    printf("%.0f\n",ans*0.5);
}
/**
10
0 0
4 0
4 1
3 1
3 3
2 3
2 2
1 2
1 3
0 3
*/

你可能感兴趣的:(算法)