clean up with sign function

This commit is contained in:
Felix Van der Jeugt 2021-12-05 11:48:31 +01:00
parent f2abc065ad
commit 87637a4682
No known key found for this signature in database
GPG Key ID: 58B209295023754D

View File

@ -10,13 +10,16 @@ function readline()
return x1, y1, x2, y2 return x1, y1, x2, y2
end end
function mark(field, x, y, count, dx, dy) function mark(field, x, y)
local i = 0 local row = field[x] or {}
while i <= count do row[y] = (row[y] or 0) + 1
local row = field[x+i*dx] or {} field[x] = row
row[y+i*dy] = (row[y+i*dy] or 0) + 1 end
field[x+i*dx] = row
i = i + 1 function sign(n)
if n == 0 then return 0
elseif n < 0 then return -1
else return 1
end end
end end
@ -24,15 +27,11 @@ io.input(arg[3])
local x1, y1, x2, y2 = readline() local x1, y1, x2, y2 = readline()
local field = {} local field = {}
while x1 ~= nil do while x1 ~= nil do
local x, y, count, dx, dy local x, y, dx, dy = x1, y1, sign(x2 - x1), sign(y2 - y1)
if x1 == x2 then mark(field, x, y)
mark(field, x1, math.min(y1, y2), math.abs(y1 - y2), 0, 1) while x ~= x2 or y ~= y2 do
elseif y1 == y2 then x, y = x + dx, y + dy
mark(field, math.min(x1, x2), y1, math.abs(x2 - x1), 1, 0) mark(field, x, y)
elseif x1 + y1 == x2 + y2 then
mark(field, math.min(x1, x2), math.max(y1, y2), math.abs(x1 - x2), 1, -1)
else
mark(field, math.min(x1, x2), math.min(y1, y2), math.abs(x1 - x2), 1, 1)
end end
x1, y1, x2, y2 = readline() x1, y1, x2, y2 = readline()
end end