Supercover DDA算法 [英] Supercover DDA algorithm
问题描述
我试图找出如何使supercover DDA算法。或者换言之,一个DDA算法,其中将包括由线交叉的所有网格点。见下面的图片。
图片由我绘制并可能不是100%准确,但它显示了总体思路。我还想说明上的图像的下半部分的实施例不具有整数的开始和结束坐标,这是必要的。
如果你需要知道,我打算用本作的视线光线投射线。
我能够实现一个典型的DDA算法,但我的问题是,我怎么可以修改它来覆盖所有的点?
谢谢!
多哈发展议程算法在Lua我目前的实施
函数鼎联(X0,Y0,X1,Y1) - 浮点输入
当地DX = X1-X0
当地DY = Y1-Y0
当地S = math.max(math.abs(DX),math.abs(DY))
DX = DX /秒
DY = DY /秒
本地X = X0
当地Y = Y0
本地I = 0
复位功能() - 迭代器用于一个for循环
如果我< = S则
当地RX,RY = X,Y
X = X + DX
Y = Y + DY
I = I + 1
返回RX,RY
结束
结束
结束
对不起,我不问问题过于频繁,主要是因为我没那么好。但我会告诉你我很擅长!解决我自己的问题! :D
作为一个说明,我的问题的图像显示穿越对角线行,如果该行通过一个点precisely,该算法不对,但经过一番思考,穿越对角线是不可取的我。
由于这的文章中,我发现了。
下面是新实施
功能线(X0,Y0,X1,Y1)
当地VX,VY = X1-X0,Y1-Y0 - 得到的差异
当地DX =的Math.sqrt(1 +(VY / VX)^ 2) - 长矢量&lt的; 1,斜率GT;
当地DY =的Math.sqrt(1 +(VX / VY)^ 2) - 长矢量&lt的1 /斜率,1>
当地IX,IY = math.floor(X0),math.floor(Y0) - 初始化起始位置
当地SX,前 - SX是增量方向
- 当然是从X0至ix的距离
如果VX< 0,则
SX = -1
EX =(X0-IX)* DX
其他
SX = 1
EX =(IX + 1-X0)* DX - 从1减去,而不是0
- 弥补地板IX
结束
当地SY,EY
如果VY< 0,则
SY = -1
EY =(Y0-IY)* DY
其他
SY = 1
EY =(IY + 1 Y0)* DY
结束
当地做= FALSE
当地的len =的Math.sqrt(VX ^ 2 + VY ^ 2)
返回函数()
如果math.min(EX,EY)< = LEN然后
当地RX,RY = IX,IY
如果前< EY然后
EX = EX + DX
九= IX + SX
其他
EY = EY + DY
IY = IY + SY
结束
返回RX,RY
ELSEIF没有做那么 - 返回最后两个坐标
做=真
返回IX,IY
结束
结束
结束
I'm trying to figure out how to make a supercover DDA algorithm. Or in other words, a DDA algorithm that will cover ALL grid points crossed by a line. See the image below.
The image was drawn by me and might not be 100% accurate but it shows the general idea. I also want to note the examples on the lower half of the image do not have integer start and end coordinates, this is necessary.
If you need to know, I intend to use this for line of sight ray casting.
I'm capable of implementing a typical DDA algorithm, but my problem is, how can I modify it to cover all points?
Thanks!
My current implementation of the DDA algorithm in Lua
function dline(x0,y0, x1,y1) -- floating point input
local dx = x1-x0
local dy = y1-y0
local s = math.max(math.abs(dx),math.abs(dy))
dx = dx/s
dy = dy/s
local x = x0
local y = y0
local i = 0
return function() -- iterator intended for a for loop
if i <= s then
local rx,ry = x,y
x = x+dx
y = y+dy
i = i+1
return rx,ry
end
end
end
Sorry, I don't ask questions too often, mainly because I'm not that good. But I'll tell you what I am good at! Solving my own problem! :D
As a note, the image in my question shows the lines crossing diagonals if the line passes through a point precisely, this algorithm does not, but after some thought, crossing diagonals is not desirable for me.
Thanks to this article I found.
Here's the new implementation
function line(x0,y0, x1,y1)
local vx,vy = x1-x0, y1-y0 -- get the differences
local dx = math.sqrt(1 + (vy/vx)^2) -- length of vector <1, slope>
local dy = math.sqrt(1 + (vx/vy)^2) -- length of vector <1/slope, 1>
local ix,iy = math.floor(x0), math.floor(y0) -- initialize starting positions
local sx,ex -- sx is the increment direction
-- ex is the distance from x0 to ix
if vx < 0 then
sx = -1
ex = (x0-ix) * dx
else
sx = 1
ex = (ix + 1-x0) * dx -- subtract from 1 instead of 0
-- to make up for flooring ix
end
local sy,ey
if vy < 0 then
sy = -1
ey = (y0-iy) * dy
else
sy = 1
ey = (iy + 1-y0) * dy
end
local done = false
local len = math.sqrt(vx^2 + vy^2)
return function()
if math.min(ex,ey) <= len then
local rx,ry = ix,iy
if ex < ey then
ex = ex + dx
ix = ix + sx
else
ey = ey + dy
iy = iy + sy
end
return rx,ry
elseif not done then -- return the final two coordinates
done = true
return ix,iy
end
end
end
这篇关于Supercover DDA算法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!