求多点的中心点坐标

来源:互联网 发布:智能应急照明灯 淘宝网 编辑:程序博客网 时间:2024/04/30 13:00
  1. /// <summary> 
  2. /// 获取中心点坐标 
  3. /// </summary> 
  4. /// <param name="p"></param> 
  5. /// <returns></returns> 
  6. public Point GetCenterPoint(Point[] p) 
  7.  
  8.     Point ptCenter = new Point(0, 0); 
  9.     int i, j; 
  10.     double ai, atmp = 0, xtmp = 0, ytmp = 0; 
  11.     if (p == null
  12.         throw new ArgumentNullException("获取多边形中心点坐标时传入的参数为空。"); 
  13.     if(p.Length == 1) 
  14.         return p[0]; 
  15.     if ((p.Length == 2) || (p.Length == 3 && p[0] == p[2])) 
  16.         return new Point((p[1].X + p[0].X) / 2, (p[1].Y + p[0].Y) / 2); 
  17.  
  18.     int n = p.Length; 
  19.     for (i = n - 1, j = 0; j < n; i = j, j++) 
  20.     { 
  21.         ai = p[i].X * p[j].Y - p[j].X * p[i].Y; 
  22.         atmp += ai; 
  23.         xtmp += (p[j].X + p[i].X) * ai; 
  24.         ytmp += (p[j].Y + p[i].Y) * ai; 
  25.     } 
  26.  
  27.     if (atmp != 0) 
  28.     { 
  29.         ptCenter.X = Convert.ToInt32(xtmp / (3 * atmp)); 
  30.         ptCenter.Y = Convert.ToInt32(ytmp / (3 * atmp)); 
  31.  
  32.     } 
  33.     return ptCenter; 
  34.  
0 0
原创粉丝点击