void ConvexHull(Point[] pts)
{
int[][] temp = new int[pts.Length][];
for (int i = 0; i < pts.Length; i++)
{
temp[i] = new int[2];
temp[i][0] = (int)(pts[i].X + pts[i].Y);
temp[i][1] = (int)(pts[i].X - pts[i].Y);
}
List<int[]> ls = temp.ToList();
int max1index = ls.FindIndex(o => o[0] == ls.Max(j => j[0]));
int max2index = ls.FindIndex(o => o[1] == ls.Max(j => j[1]));
int min1index = ls.FindIndex(o => o[0] == ls.Min(j => j[0]));
int min2index = ls.FindIndex(o => o[1] == ls.Min(j => j[1]));
Console.Write(string.Format("{0},{1},{2},{3}", max1index, min1index, max2index, min2index));
//找出点集中,最靠近点集四至的点。为了简化操作,我将点集pts的四至角点都加入了点集中,避免了外围凸包的复杂度和Voronoi切分时考虑边界
List<Point> tubao = new List<Point>();
List<Point> other = new List<Point>();
//根据最右原则,求取凸包点列表。如果像我之前将四至角点加入点集,这部分可以除去,直接构建凸包点列表
Point Pi, Pj, Pk;
int st = max1index, ed = max2index;
Pk = pts[st];
Pi = Pk;
Pj = pts[ed];
do
{
if (tubao.Count > 0)
Pj = Pk;
con:
bool right = false;
List<Point> rightpt = new List<Point>();
rightpt.Clear();
for (int i = 0; i < pts.Length; i++)
{
if (pts[i].Equals(Pi)) continue;
if (pts[i].Equals(Pj)) continue;
int re = getPtDirection(Pi, Pj, pts[i]);
if (re > 0)
{
right = true;
rightpt.Add(pts[i]);
}
else if (re == 0)
{
//共线
right = false;
}
else
{
right = false;
}
}
if (rightpt.Count == 0)
{
//更改后继点
tubao.Add(Pi);
Pi = Pj;
continue;
}
else
{
List<double> rlen = new List<double>(rightpt.Count);
foreach (var pt in rightpt)
{
rlen.Add(pt2lnDis(pt, Pi, Pj));
}
int index = rlen.FindIndex(o => o == rlen.Max());
Pj = rightpt[index];
生成泰森多边形
最新推荐文章于 2024-04-24 16:19:13 发布