标签:def 规则 res turn class -- tor math point
简单的求凸多边形面积
求不规则多边形也是类似 只要选择的点是沿着多边形边选就行了 通过容斥会得到正确答案
#include<cmath> #include<cstdio> #include<algorithm> #define db double using namespace std; const int N=1e4+50; const db eps=1e-9; struct Point { db x,y;Point(){} Point(db _x,db _y) {x=_x,y=_y;} }p[N],q[N]; Point operator - (Point A,Point B) {return Point(A.x-B.x,A.y-B.y);} db operator * (Point A,Point B) {return A.x*B.x+A.y*B.y;} db operator ^ (Point A,Point B) {return A.x*B.y-A.y*B.x;} db dist(Point a,Point b) {return sqrt((a-b)*(a-b));} bool cmp(Point a,Point b) { db c=(a-p[1])^(b-p[1]); if(fabs(c)<=eps) return dist(a,p[1])<dist(b,p[1]); return c<eps; } int main() { int n;while(scanf("%d",&n)!=EOF) { if(n==1||n==2) {puts("0");continue;} for(int i=1;i<=n;i++) { scanf("%lf%lf",&p[i].x,&p[i].y); if(i>1&&p[i].y<p[1].y) swap(p[i],p[1]); } sort(p+2,p+n+1,cmp); int ed=1; q[ed]=p[1]; for(int i=2;i<=n;i++) { while(ed>1&&((p[i]-q[ed-1])^(q[ed]-q[ed-1]))<eps) ed--; q[++ed]=p[i]; } db res=0; for(int i=2;i<ed;i++) res+=((q[i+1]-q[1])^(q[i]-q[1]))/2; // 有向面积的一半就是该三角形的面积 printf("%d\n",(int)(res/50)); } return 0; }
标签:def 规则 res turn class -- tor math point
原文地址:https://www.cnblogs.com/lxy8584099/p/10420633.html