# 计算任意多边形的面积

S_OAB = 0.5*(A_x*B_y - A_y*B_x)   【(A_x，A_y)为A点的坐标】

S_OBC = 0.5*(B_x*C_y - B_y*C_x)

S_OCD = 0.5*(C_x*D_y - C_y*D_x)

S_ODE = 0.5*(D_x*E_y - D_y*E_x)

S_OEA = 0.5*(E_x*A_y - E_y*A_x)

struct Point2d
{
double x;
double y;
Point2d(double xx, double yy): x(xx), y(yy){}
};

//计算任意多边形的面积，顶点按照顺时针或者逆时针方向排列
double ComputePolygonArea(const vector<Point2d> &points)
{
int point_num = points.size();
if(point_num < 3)return 0.0;
double s = 0;
for(int i = 0; i < point_num; ++i)
s += points[i].x * points[(i+1)%point_num].y - points[i].y * points[(i+1)%point_num].x;
return fabs(s/2.0);
}

S = S_OAB + S_OBC + S_OCD + S_ODE + S_OEA =

0.5*(A_y*(E_x-B_x) + B_y*(A_x-C_x) + C_y*(B_x-D_x) + D_y*(C_x-E_x) + E_y*(D_x-A_x))

struct Point2d
{
double x;
double y;
Point2d(double xx, double yy): x(xx), y(yy){}
};

//计算任意多边形的面积，顶点按照顺时针或者逆时针方向排列
double ComputePolygonArea(const vector<Point2d> &points)
{
int point_num = points.size();
if(point_num < 3)return 0.0;
double s = points[0].y * (points[point_num-1].x - points[1].x);
for(int i = 1; i < point_num; ++i)
s += points[i].y * (points[i-1].x - points[(i+1)%point_num].x);
return fabs(s/2.0);
}

【版权声明】转载请注明出处：http://www.cnblogs.com/TenosDoIt/p/4047211.html

posted @ 2014-10-24 00:04  tenos  阅读(...)  评论(...编辑  收藏