- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
当我的列表有很多点时,我在格雷厄姆扫描算法中遇到了问题,但每次都可以很好地处理少量点。我做了一些截图:
角度计算:
public static double angle(MyVector3D vec1, MyVector3D vec2)
{
return Math.Atan2(vec2.Y - vec1.Y, vec2.X - vec1.X) * 180 / Math.PI;
}
根据最大 Y 点按极角排序点:
//bubblesort
private void sortList()
{
double temp = 0.0;
MyVector3D tempVector = new MyVector3D();
for (int i = 1; i < points.Count; i++)
{
for (int j = 1; j < points.Count - 1; j++)
{
if (angles[j] < angles[j + 1])
{
temp = angles[j + 1];
tempVector = points[j + 1];
angles[j + 1] = angles[j];
points[j + 1] = points[j];
angles[j] = temp;
points[j] = tempVector;
}
}
}
逆时针方法:
private double ccw(MyVector3D vec1, MyVector3D vec2, MyVector3D vec3)
{
// ccwTest = ((vec2.X - vec1.X) * (vec3.Y - vec1.Y)) - ((vec2.Y - vec1.Y) * (vec3.X - vec1.X));
return ((vec2.X - vec1.X) * (vec3.Y - vec1.Y)) - ((vec2.Y - vec1.Y) * (vec3.X - vec1.X));
}
格雷厄姆扫描算法:
for (int i = 2; i < points.Count; i++)
{
while (ccw(points[M - 1], points[M], points[i]) > 0)
{
if (M > 1)
{
points.RemoveAt(M);
M -= 1;
i--;
}
else if (i == points.Count - 1)
{
break;
}
else
{
i += 1;
}
}
//goodPoints.Add(points[M]);
//listBoxInfos.Items.Add("g" + (int)points[M].X + "," + (int)points[M].Y + "," + 0);
//listBoxInfos.Items.Add("ccw" + ccwTest);
M += 1;
}
我真的不知道为什么我的程序会在 800+ 点上爆炸...很难调试,因为算法在 300,400,500...点上工作得很好。
需要信息。
最佳答案
维基百科上的算法被破坏了。它不处理多个点彼此共线以及与最小点共线的情况。例如,以下测试用例将失败:
Vector3[] points3 = new Vector3[]
{
new Vector3( 1, 1, 0),
new Vector3( 5, 5, 0),
new Vector3( 3, 3, 0),
new Vector3( 2, 2, 0),
new Vector3( 1, 1, 0),
new Vector3( 1, 10, 0),
};
问题是,当沿着点行进时,可能需要丢弃当前点而不是扩展船体或替换船体上的最后一个点,如果该点在船体中的最后两个点之间. (这只有在点也与最小点共线时才会发生,否则前面的角度排序会阻止这种双回。)在显示的伪代码中没有逻辑。
我还认为 Wikipedia 算法可能无法很好地处理浮点舍入误差。特别是检查 ccw <= 0 看起来有问题。
这是清理维基百科算法的尝试。我不得不摆脱(含糊不清的)“哨兵点”,因为如果所有点都水平对齐,它基本上会随机选择:
public static IList<Vector3> GrahamScanCompute(IList<Vector3> initialPoints)
{
if (initialPoints.Count < 2)
return initialPoints.ToList();
// Find point with minimum y; if more than one, minimize x also.
int iMin = Enumerable.Range(0, initialPoints.Count).Aggregate((jMin, jCur) =>
{
if (initialPoints[jCur].Y < initialPoints[jMin].Y)
return jCur;
if (initialPoints[jCur].Y > initialPoints[jMin].Y)
return jMin;
if (initialPoints[jCur].X < initialPoints[jMin].X)
return jCur;
return jMin;
});
// Sort them by polar angle from iMin,
var sortQuery = Enumerable.Range(0, initialPoints.Count)
.Where((i) => (i != iMin)) // Skip the min point
.Select((i) => new KeyValuePair<double, Vector3>(Math.Atan2(initialPoints[i].Y - initialPoints[iMin].Y, initialPoints[i].X - initialPoints[iMin].X), initialPoints[i]))
.OrderBy((pair) => pair.Key)
.Select((pair) => pair.Value);
List<Vector3> points = new List<Vector3>(initialPoints.Count);
points.Add(initialPoints[iMin]); // Add minimum point
points.AddRange(sortQuery); // Add the sorted points.
int M = 0;
for (int i = 1, N = points.Count; i < N; i++)
{
bool keepNewPoint = true;
if (M == 0)
{
// Find at least one point not coincident with points[0]
keepNewPoint = !NearlyEqual(points[0], points[i]);
}
else
{
while (true)
{
var flag = WhichToRemoveFromBoundary(points[M - 1], points[M], points[i]);
if (flag == RemovalFlag.None)
break;
else if (flag == RemovalFlag.MidPoint)
{
if (M > 0)
M--;
if (M == 0)
break;
}
else if (flag == RemovalFlag.EndPoint)
{
keepNewPoint = false;
break;
}
else
throw new Exception("Unknown RemovalFlag");
}
}
if (keepNewPoint)
{
M++;
Swap(points, M, i);
}
}
// points[M] is now the last point in the boundary. Remove the remainder.
points.RemoveRange(M + 1, points.Count - M - 1);
return points;
}
static void Swap<T>(IList<T> list, int i, int j)
{
if (i != j)
{
T temp = list[i];
list[i] = list[j];
list[j] = temp;
}
}
public static double RelativeTolerance { get { return 1e-10; } }
public static bool NearlyEqual(Vector3 a, Vector3 b)
{
return NearlyEqual(a.X, b.X) && NearlyEqual(a.Y, b.Y);
}
public static bool NearlyEqual(double a, double b)
{
return NearlyEqual(a, b, RelativeTolerance);
}
public static bool NearlyEqual(double a, double b, double epsilon)
{
// See here: http://floating-point-gui.de/errors/comparison/
if (a == b)
{ // shortcut, handles infinities
return true;
}
double absA = Math.Abs(a);
double absB = Math.Abs(b);
double diff = Math.Abs(a - b);
double sum = absA + absB;
if (diff < 4*double.Epsilon || sum < 4*double.Epsilon)
// a or b is zero or both are extremely close to it
// relative error is less meaningful here
return true;
// use relative error
return diff / (absA + absB) < epsilon;
}
static double CCW(Vector3 p1, Vector3 p2, Vector3 p3)
{
// Compute (p2 - p1) X (p3 - p1)
double cross1 = (p2.X - p1.X) * (p3.Y - p1.Y);
double cross2 = (p2.Y - p1.Y) * (p3.X - p1.X);
if (NearlyEqual(cross1, cross2))
return 0;
return cross1 - cross2;
}
enum RemovalFlag
{
None,
MidPoint,
EndPoint
};
static RemovalFlag WhichToRemoveFromBoundary(Vector3 p1, Vector3 p2, Vector3 p3)
{
var cross = CCW(p1, p2, p3);
if (cross < 0)
// Remove p2
return RemovalFlag.MidPoint;
if (cross > 0)
// Remove none.
return RemovalFlag.None;
// Check for being reversed using the dot product off the difference vectors.
var dotp = (p3.X - p2.X) * (p2.X - p1.X) + (p3.Y - p2.Y) * (p2.Y - p1.Y);
if (NearlyEqual(dotp, 0.0))
// Remove p2
return RemovalFlag.MidPoint;
if (dotp < 0)
// Remove p3
return RemovalFlag.EndPoint;
else
// Remove p2
return RemovalFlag.MidPoint;
}
顺便说一句,您的算法在几个地方是 n 阶的:
如果它能解决您的问题,请告诉我,我已经对其进行了一些测试,但并不完全。
关于c# - 格雷厄姆扫描问题在高点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25190164/
使用 C# (VS2008) 和 WIA - 扫描到 TIFF 格式; 当我在平板或文档进纸器上使用扫描仪扫描 1 页时,该方法执行没有任何问题。当我将多个表单加载到进纸器时,扫描第一页后执行停止(保
已关闭。此问题需要 debugging details 。目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and the
给定一个列表 :: [(Foo, Bar)] ,我想在 Bar 上执行 scanl1 s,但保留他们的 Foo “标签”。 IE。我想要一个类型为 :: [(a, b)] -> ([b] -> [c]
我有一个 HBase 表,我需要从多个范围获取结果。例如,我可能需要从不同范围获取数据,例如第 1-6 行、100-150..... 我知道对于每次扫描,我可以定义开始行和停止行。但是如果我有 6 个
我看到了这段代码。我是 C 语言的新手,所以请原谅。 while下面的循环将继续循环 if i = SIZE,则 == 是无关紧要的,因为它根本不会被执行。如果 i 小于 SIZE 那么 scanf(
这是一个关于编译过程的相当技术性的问题ABAP代码。 我知道有ABAP解析器和扫描器类实际上调用 C 内核函数来完成实际工作。然后就是代码补全事务的功能,该事务以 ABAP 列表或 XML 的形式返回
给定以下程序: int main(){ float x = non_det_float(); float y = NAN; if (isnan(y) && x == 1.0f){
我在工作中使用由供应商生成的二维码。实际上我需要通过网站手动记录所有这些项目。 QR 码包含所有这些数据,所以我想创建一个自动执行操作的应用。 例如,二维码表示“AAA|BBB|CCC|123”。我想
我有一个像这样的字符串:@"ololo width: 350px jijiji width:440px ... text=12... "我想将@"width: "之后的所有数字替换为280。所以在扫描
我在玩 scanf 时遇到了一个小问题……更具体地说,我想读取整个输入,然后忽略其余部分。让我告诉你我的意思: #include int main(void) { int number_of
我正在使用 matlab/octave 创建扫描/线性调频信号,我的结束信号似乎以错误的频率结束。我该如何修复它,以便信号以正确的频率结束。 PS:我不能在 Octave 音程中使用 chirp 命令
我正在寻找一个可以扫描 WiFi 网络并打印所有 SSID 的程序。我试过 scapy 但我失败了。我正在使用 pyCharm 编辑器。 我试过这段代码: from scapy.all import
概述 Linux 完全是用于大型服务器的最流行和最安全的操作系统之一。尽管它被广泛使用,但它仍然容易受到网络攻击。黑客以服务器为目标,窃取有价值的信息。所以迫切需要开发反黑客方法来应对安全漏洞和恶
如何获取我的 Git 存储库的某种统计信息? 我目前在 BitBucket 中托管 Git 存储库,想查找以下详细信息: 提交总数 使用过的编程语言 每种编程语言的总代码行数 您认为这可以实现吗?还是
我目前正在使用以下代码来扫描作为申请表的一部分上传的文件: $safe_path = escapeshellarg($dir . $file); $command = '/usr/bin/clamsc
我在存储库中有十几个项目。存储库结构如下所示: / ------- + project1 +------- trunk +------- tags +----
我正在使用 Dynamo DB 并想使用过滤器扫描一个表。例如,是否可以使用全局二级索引仅扫描表中的特定行? 最佳答案 这不可能!扫描始终针对基表中的所有行,当您扫描索引表作为响应时,您将仅获得该索引
我正在尝试从这里使用 SOLStumbler:Accessing & Using the MobileWiFi.framework扫描 wifi 网络。我知道苹果不支持这一点,但它是用于教育目的和实验
我知道 iPhone 蓝牙功能在 3.0 之前无法通过 SDK 访问,但是需要多长时间才能找到该区域的设备?它取决于该区域的设备数量吗?如果范围内有大约 5 个设备,扫描发现所有设备是否需要花费 30
我正在使用Elasticsearch 6.2,并且有一些查询可以分析大量文档。我正在对索引内的一个字段进行排序。 Elasticsearch检查10.000个文档(默认配置值),然后将它们分页返回。
我是一名优秀的程序员,十分优秀!