C#求點(diǎn)集的最小包圍矩形
C# 求點(diǎn)集的最小包圍矩形,供大家參考,具體內(nèi)容如下
思路:
1、求點(diǎn)集的中心點(diǎn)
2、將點(diǎn)集繞矩形進(jìn)行一系列角度的旋轉(zhuǎn),并求記錄旋轉(zhuǎn)點(diǎn)集的包圍矩形的面積和旋轉(zhuǎn)角度;
3、將面積最小的矩形繞點(diǎn)集中心點(diǎn)旋轉(zhuǎn)回去。
// 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.旋轉(zhuǎn)多邊形,針對(duì)每個(gè)點(diǎn)實(shí)現(xiàn)繞中心點(diǎn)旋轉(zhuǎn)
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; //旋轉(zhuǎn)公式
oy = (ix - cx) * Math.Sin(Q) + (iy - cy) * Math.Cos(Q) + cy;
var outpt = new XYZ(ox,oy,0);
return outpt;
}
// 3.多邊形旋轉(zhuǎn)后求簡(jiǎn)單外接矩形
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.存儲(chǔ)每個(gè)旋轉(zhuǎn)角度下多邊形的外接矩形,記錄外接矩形的頂點(diǎn)坐標(biāo)、面積和此時(shí)多邊形的旋轉(zhuǎn)角度
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; //求每個(gè)外接矩形的面積
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];
//旋轉(zhuǎ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;
}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C# Email發(fā)送郵件 對(duì)方打開郵件可獲得提醒
這篇文章主要為大家詳細(xì)介紹了C# Email發(fā)送郵件功能,對(duì)方打開通知你,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-11-11
C#?利用Autofac批量接口注入依賴的問題小結(jié)
這篇文章主要介紹了C#?利用Autofac批量接口注入依賴的問題,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-12-12
C#實(shí)現(xiàn)讀取ini配置文件的內(nèi)容
INI就是擴(kuò)展名為"INI"的文件,其實(shí)他本身是個(gè)文本文件,可以用記事本打開,本文主要為大家詳細(xì)介紹了C#實(shí)現(xiàn)讀取ini配置文件內(nèi)容的方法,需要的小伙伴可以了解下2023-12-12
淺拷貝和深拷貝深入理解(shallow copy VS deep copy)
淺拷貝和深拷貝深入理解(shallow copy VS deep copy) 本文重點(diǎn)討論引用類型變量的拷貝機(jī)制和實(shí)現(xiàn)2014-01-01
opencvsharp瑕疵檢測(cè)的實(shí)現(xiàn)示例
本文主要介紹了opencvsharp瑕疵檢測(cè)的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-05-05
C#中如何將MongoDB->RunCommand結(jié)果映射到業(yè)務(wù)類的方法總結(jié)
這篇文章主要給大家總結(jié)介紹了關(guān)于C#中如何將MongoDB->RunCommand結(jié)果映射到業(yè)務(wù)類的方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起看看吧。2018-04-04

