查找与线段相交的所有图块

Find all tiles intersected by line segment

本文关键字:段相交 查找      更新时间:2023-10-16

我必须找到所有与线段相交的图块,但布雷森汉姆的线算法不符合我的要求。我需要找到所有单元格。我不需要知道交叉点,只需要知道交叉点的事实。感谢您的帮助。

我想找到线条的方向矢量,并逐步通过瓷砖大小的划分找到单元格。但我不知道如何选择正确的步长。我认为 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           
    }
}