求複雜多邊形面積的算法

在skeinforge上看到一個計算複雜多邊形面積的python代碼,它可看成是利用2+1維的叉積的z座標的累積,非常巧妙,把它翻譯成C++代碼和大家共享,如有不足,歡迎指正。
struct Point
{
    float x, y;
};

//Get the area of a complex polygon(refer from skeinforge)
float getAreaLoop(vector<Point> loop)
{
	float areaLoopDouble=0;
	
	Point point,pointEnd;
	
	int i,length=loop.size();
	
	for(i=0;i!=length;++i)
	{
		point=loop[i];
		
		pointEnd=loop[(i + 1) % length];
		
		areaLoopDouble += point.x * pointEnd.y - pointEnd.x * point.y;
	}
	
	return 0.5 * areaLoopDouble;
}

發佈了31 篇原創文章 · 獲贊 57 · 訪問量 12萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章