设Ω是m边形(如下图),顶点
沿边界正向排列,,坐标依次为

建立Ω的多边形区域向量图。
由图知坐标原点与多边形任意相邻的两个顶点构成一个三角形,而三角形的面积可由三个顶点构成的两个平面向量的外积求得。
任意多边形的面积公式

多边形计算公式的计算和原点的选取没有关系,通常可以选点(0,0)或者多边形的第一个点(这个时候比较直观了,看起来就是把多边形分成一个个三角形和加起来,读者自己可以画个图)就可以了。

-
-
- #include <iostream>
- #include <utility>
- #include <cmath>
- using std::cout;
- using std::cin;
- using std::endl;
-
- typedef std::pair<double ,double> point;
-
- #pragma warning(disable:4244)
-
- double det(point p0,point p1,point p2)
- {
- return (p1.first-p0.first)*(p2.second-p0.second)-(p1.second-p0.second)*(p2.first-p0.first);
- }
-
- double ploygon_area(int n,point p[])
- {
- double s=0.0f;
- int i=1;
- for(;i < n-1;i++)
- s += det(p[0],p[i],p[i+1]);
- return 0.5*fabs(s);
- }
-
- int main(int argc, char *argv[])
- {
-
- int i,n;
- double s;
- point *points = NULL;
-
- cout<<"Enter the number of edges of the polygon <n>:";
- cin>>n;
- if(n < 2){
- exit(1);
- }
-
- points = (point *)malloc(n*sizeof(point));
-
- for(i=0; i<n; i++){
- cout<<endl<<"points["<<i<<"]=";
- cin>>points[i].first>>points[i].second;
- }
-
- s=ploygon_area(n, points);
- cout<<"The area is:"<<s<<std::endl;
-
- if(points)
- free(points);
-
- return 1;
- }