当前位置:  开发笔记 > 编程语言 > 正文

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

这篇文章主要为大家详细介绍了C#求点集的最小包围矩形,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

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

思路:

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

 // 1.寻找多边形的中心 
  public XYZ GetCenter(List 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 GetRect(List inpts)
  {
   var outpts =new List();
   int size = inpts.Count();
   if (size == 0)
    return null;
   else
   {
    var tempx = new List();
    var tempy = new List();
    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 boundary { get;set;}
   public XYZ center { get; set; }
   public double theta { get; set; }
   public double area { get; set; }

  }

  public RectData GetRotateRectDatas(List inpts, double theta)
  {
   
   XYZ center = GetCenter(inpts);
   var tempvertices = new List();
   for (int i=0; i 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 GetAllNewRectDatas(List inpts)
  {
   var polygens =new List();

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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

推荐阅读
个性2402852463
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有