問題情境:
針對二維不規則圖形(人體圖像),尋找重心。
思路辨析:
1.注意區分於中心。中心橫坐標是最小與最大橫坐標的均值,縱坐標亦然。
2.可以參考重心概念公式,例如橫坐標X=(x1m1+x2m2+‥+ximi)/M,其他方向坐標亦然。
解決辦法:
1.自己做的方法就是簡單把圖形看做一個點m為1。附代碼:
private PointF getCore1(List<Point> points) { PointF core = new PointF(); float sumX = 0; float sumY = 0; foreach (Point point in points) { sumX += point.X; sumY += point.Y; } core = new PointF(sumX / (float)points.Count, sumY / (float)points.Count); return core; }
2.另外一種方法,暫不解其意,如有研究者,希望解惑。附代碼:
public static PointF getCore2(List<Point> mPoints) { double area = 0.0;//多邊形面積 double Gx = 0.0, Gy = 0.0;// 重心的x、y for (int i = 1; i <= mPoints.Count; i++) { double iLat = mPoints[i % mPoints.Count].X; double iLng = mPoints[i % mPoints.Count].Y; double nextLat = mPoints[i - 1].X; double nextLng = mPoints[i - 1].Y; double temp = (iLat * nextLng - iLng * nextLat) / 2.0; area += temp; Gx += temp * (iLat + nextLat) / 3.0; Gy += temp * (iLng + nextLng) / 3.0; } Gx = Gx / area; Gy = Gy / area; return new PointF((float)Gx, (float)Gy); }
此方法,規則圖形會導致area為0,僅適用於不規則。