的是集像素? (其中GLD是在该组的任何一对点的最大直线距离)最大线性尺寸的2D组点
对于我而言,明显的为O(n^2)的解决方案是可能不够快的千点的数字。是否有很好的启发式或查找方法使时间复杂度更接近O(n)或O(log(n))?
的是集像素? (其中GLD是在该组的任何一对点的最大直线距离)最大线性尺寸的2D组点
对于我而言,明显的为O(n^2)的解决方案是可能不够快的千点的数字。是否有很好的启发式或查找方法使时间复杂度更接近O(n)或O(log(n))?
一个简单的方法是,首先找到了点,这可以在O完成的凸包(N日志N)在许多方面的时间。 [我喜欢Graham scan(见animation),但incremental算法也很受欢迎,因为是others,虽然有些采取more time]
然后你可以用任意两点开始找到最远对(直径)(比方说x和y),顺时针移动y直到离x最远,然后移动x,再移动y等等。您可以证明整个事物只需要O(n)个时间(摊销)。因此,如果使用礼品包装作为凸包算法,那么它总是O(n log n)+ O(n)= O(n log n),可能O(nh)。正如你所提到的,这个想法被称为rotating calipers。
这里是code by David Eppstein(计算几何研究员;另见他的Python Algorithms and Data Structures供将来参考)。
所有这些都不是很难编码(最多应该是100行;在上面的Python代码中应该少于50行),但在你做之前 - 你应该首先考虑你是否真的需要它。如果像你说的那样,你只有“数千个点”,那么用任何合理的编程语言都可以在不到一秒的时间内运行微不足道的O(n^2)算法(比较所有的对)。即使有一百万分,也不应超过一个小时。 :-)
你应该选择最简单的算法。
在此页面:
它表明你能够确定为O(n)凸多边形的最大直径。我只需要将我的点集首先变成凸多边形(可能使用Graham扫描)。
下面是一些C#代码,我遇到了用于计算凸包:
也许你可以画一个圆,这是比我的大多边形并慢慢收缩它,检查你是否已经与任何点相交。那么你的直径就是你正在寻找的数字。 不知道这是否是一种好方法,它听起来介于O(n)和O(n^2)之间
我的最新解决方案是尝试一种二进制分区方法,您可以在其中绘制一条线在中间并检查距该线中间的所有点的距离。 这将为您提供2推测非常远的点。然后检查这两者的距离并重复上述距离检查。重复这个过程一段时间。
我的直觉说,这是一个N为log N启发式,将让你非常接近。
我将Python代码移植到C#中。它似乎工作。
using System;
using System.Collections.Generic;
using System.Drawing;
// Based on code here:
// http://code.activestate.com/recipes/117225/
// Jared Updike ported it to C# 3 December 2008
public class Convexhull
{
// given a polygon formed by pts, return the subset of those points
// that form the convex hull of the polygon
// for integer Point structs, not float/PointF
public static Point[] ConvexHull(Point[] pts)
{
PointF[] mpts = FromPoints(pts);
PointF[] result = ConvexHull(mpts);
int n = result.Length;
Point[] ret = new Point[n];
for (int i = 0; i < n; i++)
ret[i] = new Point((int)result[i].X, (int)result[i].Y);
return ret;
}
// given a polygon formed by pts, return the subset of those points
// that form the convex hull of the polygon
public static PointF[] ConvexHull(PointF[] pts)
{
PointF[][] l_u = ConvexHull_LU(pts);
PointF[] lower = l_u[0];
PointF[] upper = l_u[1];
// Join the lower and upper hull
int nl = lower.Length;
int nu = upper.Length;
PointF[] result = new PointF[nl + nu];
for (int i = 0; i < nl; i++)
result[i] = lower[i];
for (int i = 0; i < nu; i++)
result[i + nl] = upper[i];
return result;
}
// returns the two points that form the diameter of the polygon formed by points pts
// takes and returns integer Point structs, not PointF
public static Point[] Diameter(Point[] pts)
{
PointF[] fpts = FromPoints(pts);
PointF[] maxPair = Diameter(fpts);
return new Point[] { new Point((int)maxPair[0].X, (int)maxPair[0].Y), new Point((int)maxPair[1].X, (int)maxPair[1].Y) };
}
// returns the two points that form the diameter of the polygon formed by points pts
public static PointF[] Diameter(PointF[] pts)
{
IEnumerable<Pair> pairs = RotatingCalipers(pts);
double max2 = Double.NegativeInfinity;
Pair maxPair = null;
foreach (Pair pair in pairs)
{
PointF p = pair.a;
PointF q = pair.b;
double dx = p.X - q.X;
double dy = p.Y - q.Y;
double dist2 = dx * dx + dy * dy;
if (dist2 > max2)
{
maxPair = pair;
max2 = dist2;
}
}
// return Math.Sqrt(max2);
return new PointF[] { maxPair.a, maxPair.b };
}
private static PointF[] FromPoints(Point[] pts)
{
int n = pts.Length;
PointF[] mpts = new PointF[n];
for (int i = 0; i < n; i++)
mpts[i] = new PointF(pts[i].X, pts[i].Y);
return mpts;
}
private static double Orientation(PointF p, PointF q, PointF r)
{
return (q.Y - p.Y) * (r.X - p.X) - (q.X - p.X) * (r.Y - p.Y);
}
private static void Pop<T>(List<T> l)
{
int n = l.Count;
l.RemoveAt(n - 1);
}
private static T At<T>(List<T> l, int index)
{
int n = l.Count;
if (index < 0)
return l[n + index];
return l[index];
}
private static PointF[][] ConvexHull_LU(PointF[] arr_pts)
{
List<PointF> u = new List<PointF>();
List<PointF> l = new List<PointF>();
List<PointF> pts = new List<PointF>(arr_pts.Length);
pts.AddRange(arr_pts);
pts.Sort(Compare);
foreach (PointF p in pts)
{
while (u.Count > 1 && Orientation(At(u, -2), At(u, -1), p) <= 0) Pop(u);
while (l.Count > 1 && Orientation(At(l, -2), At(l, -1), p) >= 0) Pop(l);
u.Add(p);
l.Add(p);
}
return new PointF[][] { l.ToArray(), u.ToArray() };
}
private class Pair
{
public PointF a, b;
public Pair(PointF a, PointF b)
{
this.a = a;
this.b = b;
}
}
private static IEnumerable<Pair> RotatingCalipers(PointF[] pts)
{
PointF[][] l_u = ConvexHull_LU(pts);
PointF[] lower = l_u[0];
PointF[] upper = l_u[1];
int i = 0;
int j = lower.Length - 1;
while (i < upper.Length - 1 || j > 0)
{
yield return new Pair(upper[i], lower[j]);
if (i == upper.Length - 1) j--;
else if (j == 0) i += 1;
else if ((upper[i + 1].Y - upper[i].Y) * (lower[j].X - lower[j - 1].X) >
(lower[j].Y - lower[j - 1].Y) * (upper[i + 1].X - upper[i].X))
i++;
else
j--;
}
}
private static int Compare(PointF a, PointF b)
{
if (a.X < b.X)
{
return -1;
}
else if (a.X == b.X)
{
if (a.Y < b.Y)
return -1;
else if (a.Y == b.Y)
return 0;
}
return 1;
}
}
你在哪里放置中心?可能靠近质心,但我敢打赌,我可以想出这样一种情况:该圈的中心对是否找到正确的GLD有重要影响。 – 2008-11-26 20:52:36