# Precalculated line of sight

This code is useful for doing line of sight calculations. Rather than doing the line check every frame, you can run every possible combination once and save the results to a 4-dimensional table. This also includes an example implementation.

``````

-- Use this function to perform your initial setup
function setup()
iparameter("xxx",0,12)
iparameter("yyy",0,12)

end

-- This function gets called once every frame
function draw()
background(74, 74, 74, 255)
if merr ~= 0 then map = merr end
for i = 0,16 do
for j = 0,16 do
if map[i][j]> 0 then
ellipse(33+i*44,33+j*44,11+map[i][j]/4)
end
end
end
end

-- below this, i reccomend putting in a seperate tab

end

function rou(inp)
return math.floor(inp+0.5)
end

local map = {}
for i = 0,16 do map[i] = {}
for j = 0,16 do
if vec2(i,j):len()<16 then
else
map[i][j] = 0
end
-- //you only actually need to do one quadrant and repeat it
--into the others
end end

for i = 0,12 do
for j = 0,12 do
if map[i][j] == 0 then else
local testbox = vec2(i,j)
local parentmap =  map[i][j]
for k = 0,16 do
for l = 0,16 do

local diff = vec2(k,l):normalize()
local start = vec2(0,0)
for m = 0, vec2(k,l):len() do
start = start + diff
if vec2(rou(start.x),rou(start.y))== testbox
then
parentmap[k][l] = 0
break

end
end
end
end
end
end
end
return map
end