查找与线段相交的所有图块
Find all tiles intersected by line segment
我必须找到所有与线段相交的图块,但布雷森汉姆的线算法不符合我的要求。我需要找到所有单元格。我不需要知道交叉点,只需要知道交叉点的事实。感谢您的帮助。
我想找到线条的方向矢量,并逐步通过瓷砖大小的划分找到单元格。但我不知道如何选择正确的步长。我认为 1 px 步长很糟糕。
这是Amanatides和Woo的文章"用于光线追踪的快速体素遍历算法",用于2D和3D情况。实际实施。
您可以使用
位于以下位置的众多线方程之一: http://www.cut-the-knot.org/Curriculum/Calculus/StraightLine.shtml 或 http://mathworld.wolfram.com/Line.html
假设您在坐标系中的直线经过两点,您推导出y=mx+n
方程,然后与您的瓷砖匹配,看看它们是否相交,同时在坐标系单位中向您喜欢的任何方向移动 x,从最小的 x 到最大的瓷砖。如果您的坐标系是屏幕,则 1 个像素就足够了。
这是我可以在不了解更多您面临的问题的确切性质的情况下暗示的关闭。
修改布雷森汉姆算法很容易,以便它跟踪您需要的内容。以下是该算法的相关片段:
plot(x,y);
error = error + deltaerr;
if (error >= 0.5)
{
y = y + ystep;
error = error - 1.0;
}
为了跟踪所有单元格,我们需要另一个变量。请注意,我没有严格检查这一点。
plot(x,y);
olderror = error.
error = error + deltaerr;
if (error >= 0.5)
{
y = y + ystep;
error = error - 1.0;
extra = error+olderror;
if (extra > 0)
{
plot (x,y); /* not plot (x-1,y); */
}
else if (extra < 0)
{
plot (x+1,y-1); /* not plot (x+1,y); */
}
else
{
// the line goes right through the cell corner
// either do nothing, or do both plot (x-1,y) and plot (x+1,y)
// depending on your definition of intersection
}
}
相关文章:
- 如何在数组中查找相交元素
- 如何在 c++98 中通过特定字段在 QList 中查找对象?
- 如何使用 Clang 查找所有成员字段读/写?
- 查找两个分段线性函数的相交
- 给定许多垂直或水平段及其终点的坐标(x1,x2,y1,y2),我们有多少个相交
- 查找哪个代码段比另一个代码段快
- 动态链接到 c++ 静态成员字段时符号查找失败
- C 段线相交
- 从地址中查找堆或堆块或段
- 查找两条线段是否在C++相交
- 如何在C++中编写一段代码以查找系统路径上文件的完整路径
- 通过 2 个字段查找特定对象的向量元素
- 查找与线段相交的所有图块
- 如何查找和填充第三方源的 Flash 字段
- 在CGAL中查找两个二维三角形的相交/差的三角化结果
- 您查找段错误原因的方法是什么?
- 查找无序后继递归代码中的段错误
- 在类文件和缓冲区中查找段故障
- 在这段代码中查找3个错误,这些错误都会导致无限循环
- 在容器中查找具有给定字段最小值的元素的最紧凑的方法