【问题标题】:Find all tiles intersected by line segment查找与线段相交的所有图块
【发布时间】:2012-04-27 12:01:25
【问题描述】:

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

我想找到线的方向向量,并通过平铺大小的划分逐步找到单元格。但我不知道如何选择正确的步长。我认为 1 px 步骤很糟糕。

【问题讨论】:

  • 不符合我的要求是什么意思?哪方面不适合?
  • 它不会找到所有只有杉木进入 delta 参数的单元格。查看维基百科的示例图片。

标签: c++ math optimization


【解决方案1】:

Here is article 来自 Amanatides 和 Woo “用于光线追踪的快速体素遍历算法”,适用于 2D 和 3D 案例。 Practical implementation.

【讨论】:

    【解决方案2】:

    您可以使用以下位置的众多直线方程之一:http://www.cut-the-knot.org/Curriculum/Calculus/StraightLine.shtmlhttp://mathworld.wolfram.com/Line.html

    假设您的坐标系中的线穿过两个点,您可以推导出 y=mx+n 方程并与您的图块匹配,看看它们是否相交,同时以您的坐标系为单位向您喜欢的任何方向移动 x你的瓷砖中最小的 x 直到最大。如果你的坐标系是屏幕,1个像素就够了。

    这是我可以在不了解您所面临问题的确切性质的情况下正确提示的关闭。

    【讨论】:

    • 什么是“匹配你的瓷砖”?我需要这个操作的算法。
    • 你的瓷砖是什么几何形状?
    【解决方案3】:

    修改 Bresenham 算法很容易,以便跟踪您的需求。这是算法的相关片段:

    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           
        }
    }
    

    【讨论】:

    • 我稍后会尝试,我会报告结果
    • plot (x-1,y)plot (x+1,y) 中有错误。坐标不对。它应该是plot (x,y)plot (x+1,y-1)。我已经更新了代码。
    • 此外,如果处理陡峭的线条,plot(x,y) 将变为 if (steep) plot(y,x); else plot(x,y);,并且其他 plot 调用也会发生类似变化(如维基百科文章中的第二个列表)。 `
    猜你喜欢
    • 2013-06-12
    • 2017-11-25
    • 2012-05-18
    • 1970-01-01
    • 1970-01-01
    • 2016-12-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多