题解:
求多边形面积
分成很多块三角形求就可以了
凹的也是支持的
代码:
#include <bits/stdc++.h> using namespace std; #define rint register int #define IL inline #define rep(i,h,t) for (int i=h;i<=t;i++) #define dep(i,t,h) for (int i=t;i>=h;i--) const double eps=1e-8; struct Point { double x,y; Point(double x1,double y1) {x=x1,y=y1;} Point(){}; Point operator +(const Point b) const { return Point(x+b.x,y+b.y); } Point operator -(const Point b) const { return Point(x-b.x,y-b.y); } double operator *(const Point b) const { return x*b.x+y*b.y; } double operator ^(const Point b) const { return x*b.y-y*b.x; } Point operator *(double k) { return Point(x*k,y*k); } Point operator /(double k) { return Point(x/k,y/k); } bool operator ==(Point b) { return b.x==x&&b.y==y?1:0; } }; struct Line { Point x,y; Line() {}; Line(Point x1,Point y1){x=x1,y=y1;}; }; double lenth(Point x) { return sqrt(x.x*x.x+x.y*x.y); } double angle(Point x,Point y) { return acos(x*y/lenth(x)/lenth(y)); } int dcmp(double x) { if (x<-eps) return(-1); else if (x>eps) return(1); else return(0); } Point rotate(Point x,double r) { return Point(x.x*cos(r)-x.y*sin(r),x.x*sin(r)+x.y*cos(r)); } Point gtp(Line x,Line y) { Point v1=x.y-x.x; Point v2=y.y-y.x; double k=((x.x^v1)-(y.x^v1))/(v2^v1); return y.x+v2*k; } double distance(Point x,Line y) { Point p1=x-y.x,p2=y.y-y.x; return fabs((p1^p2)/lenth(p2)); } const int N=1000; Point p[N]; int main() { freopen("1.in","r",stdin); freopen("1.out","w",stdout); ios::sync_with_stdio(false); int n; while (cin>>n&&n) { rep(i,1,n) { int x,y; cin>>x>>y; p[i]=Point(x,y); } double ans=0; rep(i,2,n-1) ans+=(p[i]-p[1])^(p[i+1]-p[1]); ans/=2; printf("%.1lf\n",ans); } return 0; }
原文:https://www.cnblogs.com/yinwuxiao/p/9992532.html