多边形面积[nyoj 1011,hdu 2036]

nyoj 1101 So Easy[II]:点击打开链接

hdu 2036 改革春风吹满地:点击打开链接

 求多边形的面积的方法,就是利用叉积的几何意义:以两向量为邻边的平行四边形的有向面积。

nyoj 这道题是按顺时针给出的点。而hdu上的按逆时针的顺序给出的。其实无所谓,最后求出的面积都是正的。

code:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;

const int N = 1e2 + 4;
struct Point{
    double x, y;
}p[N];

struct Vector{
    double x, y;
};

double operator *(Vector a, Vector b){
    return a.x * b.y - b.x * a.y;
}
int n;
double f(){
    double sum = 0;
    Vector a, b;
    for(int i = 1; i < n - 1; i ++){
        a.x = p[i].x - p[0].x; a.y = p[i].y - p[0].y;
        b.x = p[i + 1].x - p[0].x; b.y = p[i + 1].y - p[0].y;
        sum += (a * b) * 0.5;
    }
    return sum;
}
int main(){
    while(~scanf("%d",&n)){
        for(int i = 0; i < n; i ++){
            scanf("%lf %lf", &p[i].x, &p[i].y);
        }
        double ans = f();
        printf("%.2lf\n", fabs(ans));
    }
    return 0;
}


计算几何要多积累一些模板。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值