Esempio n. 1
0
        /// <summary>
        /// Calculates the centroid of the polygon by moving it according to each holes
        /// weighted centroid.
        /// </summary>
        public C2DPoint GetCentroid()
        {
            C2DPoint HoleCen = new C2DPoint(0, 0);

            if (_Holes.Count == 0)
            {
                return(Rim.GetCentroid());
            }


            C2DPoint PolyCen = Rim.GetCentroid();

            double dPolyArea = Rim.GetArea();
            double dHoleArea = 0;

            for (int i = 0; i < _Holes.Count; i++)
            {
                dHoleArea += GetHole(i).GetArea();
            }


            if (dHoleArea == 0 || dHoleArea == dPolyArea)
            {
                return(Rim.GetCentroid());
            }
            else
            {
                for (int i = 0; i < _Holes.Count; i++)
                {
                    C2DPoint pt = GetHole(i).GetCentroid();
                    pt.Multiply(GetHole(i).GetArea() / dHoleArea);
                    HoleCen += pt;
                }
            }

            C2DVector Vec = new C2DVector(HoleCen, PolyCen);

            Vec.Multiply(dHoleArea / (dPolyArea - dHoleArea));

            PolyCen.Move(Vec);

            return(PolyCen);
        }