Skip to content

Instantly share code, notes, and snippets.

@Deco
Created June 9, 2013 01:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Deco/5737229 to your computer and use it in GitHub Desktop.
Save Deco/5737229 to your computer and use it in GitHub Desktop.
Live recursive contour plotting for Lua
local function contourPlot(func, lf, x, y, w, h, interval, offset, mindepth, maxdepth, dbg, depth, fvbr, fvbl, fvtl, fvtr)
local wh, hh = w/2, h/2
local v_c = func(x+wh, y+hh)+offset
local v_br = fvbr or func(x+w , y+h )+offset
local v_bl = fvbl or func(x , y+h )+offset
local v_tl = fvtl or func(x , y )+offset
local v_tr = fvtr or func(x+w , y )+offset
local linelist, linecount, v = {}, 1
v = math.floor(v_c /interval)
linelist[v] = true
v = math.floor(v_br/interval)
if not linelist[v] then linelist[v] = true linecount = linecount+1 end
v = math.floor(v_bl/interval)
if not linelist[v] then linelist[v] = true linecount = linecount+1 end
v = math.floor(v_tl/interval)
if not linelist[v] then linelist[v] = true linecount = linecount+1 end
v = math.floor(v_tr/interval)
if not linelist[v] then linelist[v] = true linecount = linecount+1 end
--if depth ~= nil then print(depth) end
--if depth == nil then DBGMSG{v_c, v_br, v_bl, v_tl, v_tr} end
local newdepth = (depth or 0)+1
if linecount > 1 or newdepth < mindepth then
if dbg then
love_graphics.setColor(0, 255, 0, 255)
love_graphics.setLineWidth(1)
love_graphics.rectangle("line", x+4, y+4, w-4, h-4)
end
if newdepth <= maxdepth then
local newmindepth = linecount > 1 and newdepth+2 or mindepth
--[[br]]contourPlot(func, lf, x+wh, y+hh, wh, hh, interval, offset, newmindepth, maxdepth, dbg, newdepth, v_br, nil , v_c , nil )
--[[bl]]contourPlot(func, lf, x , y+hh, wh, hh, interval, offset, newmindepth, maxdepth, dbg, newdepth, nil , v_bl, nil , v_c )
--[[tl]]contourPlot(func, lf, x , y , wh, hh, interval, offset, newmindepth, maxdepth, dbg, newdepth, v_c , nil , v_tl, nil )
--[[tr]]contourPlot(func, lf, x+wh, y , wh, hh, interval, offset, newmindepth, maxdepth, dbg, newdepth, nil , v_c , nil , v_tr)
else
for line, _ in pairs(linelist) do
local v_l = line*interval
local interest = {}
local d_br = v_l-v_br local sign_br = d_br<0 and -1 or d_br>0 and 1 or 0
local d_bl = v_l-v_bl local sign_bl = d_bl<0 and -1 or d_bl>0 and 1 or 0
local d_tl = v_l-v_tl local sign_tl = d_tl<0 and -1 or d_tl>0 and 1 or 0
local d_tr = v_l-v_tr local sign_tr = d_tr<0 and -1 or d_tr>0 and 1 or 0
-- clockwise
-- right edge
if sign_tr ~= sign_br then
table.insert(interest, w)
table.insert(interest, h*(d_tr)/(v_br-v_tr))
end
-- bottom edge
if sign_br ~= sign_bl then
table.insert(interest, w*(d_bl)/(v_br-v_bl))
table.insert(interest, h)
end
-- left edge
if sign_bl ~= sign_tl then
table.insert(interest, 0)
table.insert(interest, h*(d_tl)/(v_bl-v_tl))
end
-- top edge
if sign_tl ~= sign_tr then
table.insert(interest, w*(d_tl)/(v_tr-v_tl))
table.insert(interest, 0)
end
if #interest == 0 then
--
elseif #interest == 4 then
lf(
x+interest[1], y+interest[2],
x+interest[3], y+interest[4]
)
elseif #interest == 8 then
lf(
x+interest[1], y+interest[2],
x+interest[5], y+interest[6]
)
lf(
x+interest[2], y+interest[3],
x+interest[7], y+interest[8]
)
else
error("wat: "..(#interest))
end
end
end
elseif dbg then
love_graphics.setColor(0, 100, 0, 255)
love_graphics.setLineWidth(1)
love_graphics.rectangle("line", x+4, y+4, w-4, h-4)
end
end
util.contourPlot = contourPlot
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment