1018 - 计算几何之多边形面积 - 改革春风吹满地 (HDU 2036)

版权声明:虽然我只是个小蒟蒻但转载也请注明出处哦 https://blog.csdn.net/weixin_42557561/article/details/83145961

传送门

分析

sb题

主要是题面太皮,我才去做的:)

简单的多边形面积,用叉积即可

代码

#include<bits/stdc++.h>
#define in read()
#define N 105
using namespace std;
inline int read(){
	char ch;int f=1,res=0;
	while((ch=getchar())<'0'||ch>'9') if(ch=='-') f=-1;
	while(ch>='0'&&ch<='9'){
		res=(res<<3)+(res<<1)+ch-'0';
		ch=getchar();
	}
	return f==1?res:-res;
}
struct point{
	double x,y;
	point(){}
	point(double _x,double _y):x(_x),y(_y){}
	friend inline point operator +(const point &a,const point &b){return point(a.x+b.x,a.y+b.y);}
	friend inline point operator -(const point &a,const point &b){return point(a.x-b.x,a.y-b.y);}
	friend inline point operator *(double k,const point &a){return point(a.x*k,a.y*k);}
	friend inline double dot(const point &a,const point &b){return a.x*b.x+a.y*b.y;}
	friend inline double cross(const point &a,const point &b){return a.x*b.y-a.y*b.x;}
	friend inline double len(const point &a){return sqrt(dot(a,a));	}
	friend inline double dis(const point &a,const point &b){return len(a-b);}
}a[N];
int n;
int main(){
	while(1){
		n=in;
		if(!n) break;
		int i,j,k;
		for(i=1;i<=n;++i) a[i].x=in,a[i].y=in;
		double ans=0;
		a[n+1]=a[1];
		for(i=1;i<=n;++i)	ans+=cross(a[i],a[i+1]);
		printf("%.1lf\n",fabs(ans/2.0));
	}
	
	return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_42557561/article/details/83145961