亲宝软件园·资讯

展开

C#求点集的最小包围矩形

侠客行江湖 人气:5

本文着重为大家仔细讲解了C#求点集的最小包围矩形,文中代码实例讲解的非常细致,希望能够帮助到您,欢迎大家阅读和收藏

C# 求点集的最小包围矩形,供大家参考,具体内容如下

思路:

1、求点集的中心点
2、将点集绕矩形进行一系列角度的旋转,并求记录旋转点集的包围矩形的面积和旋转角度;
3、将面积最小的矩形绕点集中心点旋转回去。

 // 1.寻找多边形的中心 
  public XYZ GetCenter(List<XYZ> pts)
  {
   double sumx = 0;
   double sumy = 0;
   foreach (var p in pts)
   {
    sumx = sumx + p.X;
    sumy = sumy + p.Y;
   }
   var pt = new XYZ(sumx/pts.Count(),sumy/pts.Count(),0);
   return pt;
  }

  // 2.旋转多边形,针对每个点实现绕中心点旋转

  public XYZ RotatePt(XYZ inpt ,XYZ centerPt ,double theta)
  {
   double ix = inpt.X;
   double iy = inpt.Y;
   double cx = centerPt.X;
   double cy = centerPt.Y;
   double Q = theta / 180 * 3.1415926; //角度

   double ox, oy;
   ox = (ix - cx) * Math.Cos(Q) - (iy - cy) * Math.Sin(Q) + cx; //旋转公式
   oy = (ix - cx) * Math.Sin(Q) + (iy - cy) * Math.Cos(Q) + cy;

   var outpt = new XYZ(ox,oy,0);
   return outpt;
  }

  // 3.多边形旋转后求简单外接矩形

  public List<XYZ> GetRect(List<XYZ> inpts)
  {
   var outpts =new List<XYZ>();
   int size = inpts.Count();
   if (size == 0)
    return null;
   else
   {
    var tempx = new List<double>();
    var tempy = new List<double>();
    for (int i = 0; i < size; i++)
    {
     tempx.Add(inpts[i].X);
     tempy.Add(inpts[i].Y);
    }

    XYZ endpoint0 = new XYZ(tempx.Min(), tempy.Max(), 0);
    XYZ endpoint1 = new XYZ(tempx.Max(), tempy.Max(), 0);
    XYZ endpoint2 = new XYZ(tempx.Max(), tempy.Min(), 0);
    XYZ endpoint3 = new XYZ(tempx.Min(), tempy.Min(), 0);
    outpts.Add(endpoint0);
    outpts.Add(endpoint1);
    outpts.Add(endpoint2);
    outpts.Add(endpoint3);
    return outpts;
   }
  }
  // 4.存储每个旋转角度下多边形的外接矩形,记录外接矩形的顶点坐标、面积和此时多边形的旋转角度

  public class RectData
  {
   public List<XYZ> boundary { get;set;}
   public XYZ center { get; set; }
   public double theta { get; set; }
   public double area { get; set; }

  }

  public RectData GetRotateRectDatas(List<XYZ> inpts, double theta)
  {
   
   XYZ center = GetCenter(inpts);
   var tempvertices = new List<XYZ>();
   for (int i=0; i<inpts.Count();i++)
   {
    XYZ temp = RotatePt(inpts[i], center, theta);
    tempvertices.Add(temp);
   }
   List<XYZ> vertices = GetRect(tempvertices);
   double deltaX, deltaY;      //求每个外接矩形的面积
   deltaX = vertices[0].X - vertices[2].X;
   deltaY = vertices[0].Y - vertices[2].Y;

   var polygen = new RectData
   {
    area=Math.Abs(deltaY * deltaX),
    center= center,
    theta = theta,
    boundary= vertices
   };
   return polygen;
  }

  //获取所有新的矩形
  public List<RectData> GetAllNewRectDatas(List<XYZ> inpts)
  {
   var polygens =new List<RectData>();

   for (int theta = 0; theta <= 90;)
   {
    polygens.Add(GetRotateRectDatas(inpts, theta));
    theta = theta + 5;
   }
   return polygens;
  }
  //获取新的矩形
  public RectData GetMinAreaRect(List<RectData> polygons)
  {

   double minarea = 100000000;
   int N =0;
   for ( int i=0; i< polygons.Count(); i++)
   {
    if (minarea > polygons[i].area)
    {
     minarea = polygons[i].area;
     N = i;
    }
   }
   var polygon = new RectData();
   polygon = polygons[N];

   //旋转到最小面积的方向
   XYZ centerPt = GetCenter(polygon.boundary);
   var boundary = new List<XYZ>();
   foreach(var bound in polygon.boundary)
   {
    XYZ pt = RotatePt(bound, polygon.center, -polygon.theta);
    boundary.Add(pt);
   }
   var outpolygon = new RectData
   {
    center= polygon.center,
    area = polygon.area,
    theta = polygon.theta,
    boundary = boundary
   };
   return outpolygon;
}

加载全部内容

相关教程
猜你喜欢
用户评论