首先,求一个n边形的面积,顶点(p1,p2,…pn),这些点必须是按顺时针或逆时针排列,也就是点与点之间必须相邻。这里只谈公式我自己用的
至于这个公式的原理,在这里推荐一个链接:https://blog.csdn.net/LemonGirl131/article/details/51130659?utm_medium=distribute.pc_relevant_t0.none-task-blog-BlogCommendFromMachineLearnPai2-1.nonecase&depth_1-utm_source=distribute.pc_relevant_t0.none-task-blog-BlogCommendFromMachineLearnPai2-1.nonecase
还有就是这里有一道模板题,代码可一参考一下。
Description
Calculus is the first lesson Jason had. It is tedious, so an interesting idea occurred to Jason. He took out his mobile phone and open the HDUOJ and found a water problem. He said to himself, “I must solve this f**king easy water problem in ten minutes! ”.
You were invited to compete with him online. Don’t be defeated.
Input
Multiple test cases, Each test first contains an integer N(3<=N<=100000) indicating the number of vertices of the polygon. The i-th of the following N lines contains two integers xi and yi (0<=xi,yi<=10^9) separated by a blank. (xi,yi) is the i-th vertex of the polygon, and (x1,y1),…,(xn,yn) will be in counterclockwise order.
Output
For each test case, output a number as the answer which is the area of the polygon be multiplied by 2. In case the answer is greater than 1000000006, please modulo the answer with 1000000007.
Sample Input
3
0 0
1 0
0 1
4
0 0
1 0
1 1
0 1
Sample Output
1
2
#include <cstdio>
#include <algorithm>
#include <iostream>
#include <cmath>
#include <cstring>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <vector>
#include <ctype.h>
using namespace std;
typedef long long ll;
const ll N=10005;
const ll inf=1 << 29;
const ll mod=1e9+7;
#define mem(a,b) memset(a,b,sizeof(a))
struct node {
ll x;
ll y;
}a[100005];
int main()
{
int n;
while(~scanf("%d",&n))
{
mem(a,0);
for(int i=1;i<=n;++i)
{
scanf("%lld %lld",&a[i].x,&a[i].y);
}
ll ans=0;
for(int i=1;i<=n-1;++i)
{
ans=(ans+(a[i].x*a[i+1].y-a[i+1].x*a[i].y)%mod+mod)%mod;
}
ans=(ans+(a[n].x*a[1].y-a[1].x*a[n].y)%mod+mod)%mod;
printf("%lld\n",ans);
}
return 0;
}