问题情境:
针对二维不规则图形(人体图像),寻找重心。
思路辨析:
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,仅适用于不规则。