update the vector.line function

This commit is contained in:
HybridDog 2014-09-14 17:15:00 +02:00
parent 2c18e641f1
commit a34d7eb231

View File

@ -127,12 +127,25 @@ function vector.fine_line(pos, dir, range, scale)
return return_fine_line(pos, dir, range, scale) return return_fine_line(pos, dir, range, scale)
end end
function vector.line(pos, dir, range) function vector.line(pos, dir, range, alt)
--assert_vector(pos) --assert_vector(pos)
if not range then --dir = pos2 if alt then
dir, range = vector.direction(pos, dir), vector.distance(pos, dir) if not range then --dir = pos2
dir, range = vector.direction(pos, dir), vector.distance(pos, dir)
end
return return_line(pos, dir, range)
end end
return return_line(pos, dir, range) if range then --dir = pos2
dir = vector.round(vector.multiply(dir, range))
else
dir = vector.subtract(dir, pos)
end
local line,n = {},1
for _,i in ipairs(vector.threeline(dir.x, dir.y, dir.z)) do
line[n] = {x=pos.x+i[1], y=pos.y+i[2], z=pos.z+i[3]}
n = n+1
end
return line
end end
local twolines = {} local twolines = {}
@ -142,7 +155,8 @@ function vector.twoline(x, y)
if line then if line then
return line return line
end end
line,n = {},1 line = {}
local n = 1
local dirx = 1 local dirx = 1
if x < 0 then if x < 0 then
dirx = -dirx dirx = -dirx
@ -168,6 +182,42 @@ function vector.twoline(x, y)
return line return line
end end
local threelines = {}
function vector.threeline(x, y, z)
local pstr = x.." "..y.." "..z
local line = threelines[pstr]
if line then
return line
end
if x ~= math.floor(x) then
print("[vector_extras] INFO: The position used for vector.threeline isn't round.")
end
local two_line = vector.twoline(x, y)
line = {}
local n = 1
local zmin, zmax = 0, z
if z < 0 then
zmin, zmax = zmax, zmin
end
local m = z/math.hypot(x, y)
local dir = 1
if m < 0 then
dir = -dir
end
for _,i in ipairs(two_line) do
local px, py = unpack(i)
local ph = math.hypot(px, py)
local z1 = math.max(math.min(math.floor((ph-0.5)*m+0.5), zmax), zmin)
local z2 = math.max(math.min(math.floor((ph+0.5)*m+0.5), zmax), zmin)
for pz = z1,z2,dir do
line[n] = {px, py, pz}
n = n+1
end
end
threelines[pstr] = line
return line
end
function vector.straightdelay(s, v, a) function vector.straightdelay(s, v, a)
if not a then if not a then
return s/v return s/v