按逆时针方向依次给出多边形 \(n\) 个顶点,求多边形的面积。
公式:
(其中 \(P_{n+1}=P_1\))
不太会证明可以感性理解。任取一点 \(O\),可以发现多边形内的面积被扫过一遍,多边形外的面积被扫过两边(其中一正一负抵消)。然后你直接取 \(O\) 在原点很方便。
代码:
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std;
const int N=100009;
const double eps=1e-10;
int n;
struct Point
{
double x,y;
Point() {}
Point(double X,double Y) : x(X),y(Y) {}
Point operator - (const Point a)const { return Point(x-a.x,y-a.y); }
double operator * (const Point a)const { return x*a.y-y*a.x; }
void read() { scanf("%lf %lf",&x,&y); }
void print() { printf("%.2lf %.2lf\n",x,y); }
}a[N],Q;
int nxt(int x) { return x==n?1:x+1; }
void init()
{
scanf("%d",&n);
for (int i=1;i<=n;i++)
a[i].read();
}
void work()
{
double area=0;
for (int i=1;i<=n;i++)
area+=(a[i]*a[nxt(i)])/2;
printf("%.2lf\n",area);
}
int main()
{
init();
work();
return 0;
}
原文:https://www.cnblogs.com/With-penguin/p/13202977.html