求多点的中心点坐标

来源:互联网 发布:windows 7 系统 编辑:程序博客网 时间:2024/04/30 14:02
        /// <summary>        /// 获取中心点坐标        /// </summary>        /// <param name="p"></param>        /// <returns></returns>        public Point GetCenterPoint(Point[] p)        {            Point ptCenter = new Point(0, 0);            int i, j;            double ai, atmp = 0, xtmp = 0, ytmp = 0;            if (p == null)                throw new ArgumentNullException("获取多边形中心点坐标时传入的参数为空。");            if(p.Length == 1)                return p[0];            if ((p.Length == 2) || (p.Length == 3 && p[0] == p[2]))                return new Point((p[1].X + p[0].X) / 2, (p[1].Y + p[0].Y) / 2);            int n = p.Length;            for (i = n - 1, j = 0; j < n; i = j, j++)            {                ai = p[i].X * p[j].Y - p[j].X * p[i].Y;                atmp += ai;                xtmp += (p[j].X + p[i].X) * ai;                ytmp += (p[j].Y + p[i].Y) * ai;            }            if (atmp != 0)            {                ptCenter.X = Convert.ToInt32(xtmp / (3 * atmp));                ptCenter.Y = Convert.ToInt32(ytmp / (3 * atmp));            }            return ptCenter;        }