求叉积

版权声明:本文为博主原创文章,未经博主允许必须转载。 https://blog.csdn.net/qq_35950004/article/details/83717634

求任意多边形的面积

#include<cstdio>
#include<cstring>
#include<cctype>
#include<cmath>
#include<algorithm>
#define maxn 105
using namespace std;

int n;

struct Point{
	double x,y;
	Point(){}
	Point(double a,double b){x=a,y=b;}
}P[maxn];

typedef Point Vector;

Vector operator -(const Point A,const Point B){ return Point(A.x-B.x,A.y-B.y); }

double Cross(Point A,Point B){
	return A.x*B.y-A.y*B.x;
}

int main(){
	scanf("%d",&n);
	for(int i=1;i<=n;i++) scanf("%lf%lf",&P[i].x,&P[i].y);
	
	double ans=0;
	for(int i=2;i<n;i++)
		ans+=Cross(P[i+1]-P[1],P[i]-P[1]);
		
	printf("%.1lf",fabs(ans)/2);
}

猜你喜欢

转载自blog.csdn.net/qq_35950004/article/details/83717634