题目大意:逆时针方向给出多边形边数以及各个顶点坐标,求多边形面积。
解题思路:这是一道计算几何入门题,直接就是模板题,但要注意顶点输入要按照逆时针方向,该题不存在这个问题。详见code。
题目来源:http://acm.hdu.edu.cn/showproblem.php?pid=2036
code:
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;
const int MAXN=100+10;
int n;
struct node{
double x,y;
}p[MAXN];
double areaofp(node p[],int n){ //计算几何模板
double area=0;
for(int i=1;i<=n;i++)
area+=p[i-1].x*p[i%n].y-p[i-1].y*p[i%n].x;
return fabs(area/2.0); //输出绝对值,防止产生负数
}
int main(){
while(scanf("%d",&n)!=EOF && n){
for(int i=0;i<n;i++) //接收各点坐标,注意这里必须要逆时针方向输入顶点坐标
scanf("%lf%lf",&p[i].x,&p[i].y);
printf("%.1lf\n",areaofp(p,n));
}
return 0;
}
本文介绍了一种计算多边形面积的方法,适用于逆时针方向给出的多边形顶点坐标。通过具体实例展示了如何利用C++实现这一计算过程,并提供了完整的代码示例。
845

被折叠的 条评论
为什么被折叠?



