Irregular centroid

Problem situation:

  For two-dimensional irregular shapes (body image), find the center of gravity.

Analysis of ideas:

  1. The distinction in the center. The abscissa is the center of the minimum and the maximum mean abscissa, ordinate versa.

  2. The center of gravity can refer to the concept of formula, e.g. abscissa X = (x1m1 + x2m2 + ‥ + ximi) / M, the other direction of the coordinate versa.

Solution:

  1. The method of their own to do is simply to be seen as a graphical point is 1 m. With code:

        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. Another way to temporarily understand its meaning, if researchers want to doubts. With code:

        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);
        }

  This method, the rules will lead to the graphics area to 0, only for irregular.

Guess you like

Origin www.cnblogs.com/gaara-zhang/p/12394668.html