不规则多边形的面积计算
1、C++ 方法
template <class T>
T Convex_Hull_Volume(std::vector<T> &x, std::vector<T> &y)
{
if(x.size() != y.size()){
std::cout<<"Err: num mismatch:"<<x.size()<<" != "<<y.size()<<std::endl;
return 0.0;
}
std::vector<T> xp(x.size()+1);
std::vector<T> yp(y.size()+1);
xp.assign(x.begin(), x.end());
yp.assign(y.begin(), y.end());
xp.push_back(xp[0]);
yp.push_back(yp[0]);
T sumx = 0.0, sumy = 0.0;
for(int k=0; k<x.size(); k++){
sumx += xp[k]*yp[k+1];
sumy += xp[k+1]*yp[k];
}
return (std::abs(sumx-sumy)/2.0);
}
2、Python方法
先安装包: pip3 install polygon-math
from polygon_math import polygon
Vertices = [[x0,y0],[x1,y1],[x2,y2],...] # 2D-coordinates of vertices
pg = polygon(Vertices)
pg.Area #多边形面积
评论已关闭