From 5456e17c8b787dba50ba5a4c22087ec33d042f47 Mon Sep 17 00:00:00 2001 From: notwa Date: Mon, 11 Mar 2013 15:09:01 +0000 Subject: [PATCH 1/2] --- main.lua | 144 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 144 insertions(+) create mode 100644 main.lua diff --git a/main.lua b/main.lua new file mode 100644 index 0000000..cff7b57 --- /dev/null +++ b/main.lua @@ -0,0 +1,144 @@ +function lerp(x0, x1, t) + return x0*(1-t) + x1*t +end + +function blendExp(x0, x1, t) + return x0^(1-t)*x1^t +end + +local sw = 640 +local sh = 480 + +local font +local fs = 12 + +local coordsA = {} +local coordsB = {} +local coordsAL = {} +local coordsBL = {} + +local input = { + 0.9013, + 1.3685, + 1.1046, + 1.1633, + 0.7332, + 0.8603, +} + +local win = { + T = 1.5, + B = -0.5, + L = 0, + R = 1, + stepX = 0.1, + stepY = 0.25, +} +win.scaleX = 1/(win.L - win.R) +win.scaleY = 1/(win.T - win.B) + +local precision = 32 + +function screenX(x) + return sw*(win.L-x)*win.scaleX +end + +function screenY(y) + return sh*(win.T-y)*win.scaleY +end + +function addPoint(coords, x, y) + coords[#coords+1] = screenX(x) + coords[#coords+1] = screenY(y) +end + +function interpolate(x, values, interpolator) + local x = (x*#values) % #values + local x0 = math.floor(x) + local x1 = (x0 + 1) % #values + return interpolator(values[x0+1], values[x1+1], x - x0) +end + +function love.load() + if not love.graphics.setMode(sw, sh) then + error("couldn't setMode ("..sw.." by "..sh..")") + end + + love.graphics.setCaption("hey it's a graph") + + --font = love.graphics.newFont("DejaVuSansMono.ttf", fs) + --love.graphics.setFont(font) + + local lines = #input * precision + local seg = 1/lines + + for n=1, lines+1 do + local x = (n-1)*seg + local hue = (n-1)/lines + + local y = interpolate(hue, input, lerp) + addPoint(coordsA, x, y) + addPoint(coordsAL, x, math.log(y)) + + y = interpolate(hue, input, blendExp) + addPoint(coordsB, x, y) + addPoint(coordsBL, x, math.log(y)) + end +end + +function love.keypressed(key) + if key == "escape" then + love.event.push("quit") + end +end + +local drawCoords = love.graphics.line +local drawText = love.graphics.print + +function color(hex) + local r = tonumber(string.sub(hex, 1,2), 16) + local g = tonumber(string.sub(hex, 3,4), 16) + local b = tonumber(string.sub(hex, 5,6), 16) + love.graphics.setColor(r,g,b) +end + +function drawVert(x) + drawCoords(x, 0, x, sh) +end + +function drawHoriz(y) + drawCoords(0, y, sw, y) +end + +function love.draw() + love.graphics.setBackgroundColor(255,255,255) + love.graphics.clear() + + for x=win.L, win.R, win.stepX do + color("d3e2e7") + local rx = screenX(x) + drawVert(rx) + color("808080") + drawText(tostring(x), rx, sh - fs) + end + + for y=win.B, win.T, win.stepY do + color("c6cce3") + local ry = screenY(y) + drawHoriz(ry) + color("808080") + drawText(tostring(y), 0, ry) + end + + color("404040") + drawVert(screenX(0)) + drawHoriz(screenY(0)) + + color("57C1BB") + drawCoords(coordsA) + drawCoords(coordsAL) + + color("9F65C5") + drawCoords(coordsB) + drawCoords(coordsBL) +end From 39c1899843776cdb2a1076ff4ee6fa9e0e3cc02e Mon Sep 17 00:00:00 2001 From: notwa Date: Wed, 13 Mar 2013 11:44:09 +0000 Subject: [PATCH 2/2] --- main.lua | 71 ++++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 46 insertions(+), 25 deletions(-) diff --git a/main.lua b/main.lua index cff7b57..b54ec47 100644 --- a/main.lua +++ b/main.lua @@ -1,5 +1,5 @@ function lerp(x0, x1, t) - return x0*(1-t) + x1*t + return x0*(1-t) + x1*t end function blendExp(x0, x1, t) @@ -18,27 +18,27 @@ local coordsAL = {} local coordsBL = {} local input = { - 0.9013, - 1.3685, - 1.1046, - 1.1633, - 0.7332, - 0.8603, + [0]=0.9013, + [60]=1.3685, + [120]=1.1046, + [180]=1.1633, + [240]=0.7332, + [300]=0.8603, + [360]=0.9013, } +local xPrecision = 60/20 local win = { T = 1.5, B = -0.5, L = 0, - R = 1, - stepX = 0.1, + R = 360, + stepX = 30, stepY = 0.25, } win.scaleX = 1/(win.L - win.R) win.scaleY = 1/(win.T - win.B) -local precision = 32 - function screenX(x) return sw*(win.L-x)*win.scaleX end @@ -52,11 +52,33 @@ function addPoint(coords, x, y) coords[#coords+1] = screenY(y) end -function interpolate(x, values, interpolator) - local x = (x*#values) % #values - local x0 = math.floor(x) - local x1 = (x0 + 1) % #values - return interpolator(values[x0+1], values[x1+1], x - x0) +function index(t, x) + if type(x) ~= type(0) then + return nil + end + + local x0 + local x1 + for tx, ty in pairs(t) do + -- we can't guarantee the order the key/values come in + if tx ~= "interpolator" then + -- x0 = largest tx that doesn't go over x + if tx <= x and (x0 == nil or tx > x0) then + x0 = tx + end + -- x1 = smallest tx that doesn't go under x + if tx > x and (x1 == nil or tx < x1) then + x1 = tx + end + end + end + x1 = x1 or x0 + + local y0 = rawget(t, x0) + local y1 = rawget(t, x1) + local f = rawget(t, "interpolator") + local y = f(y0, y1, (x - x0)/(x1 - x0)) + return y end function love.load() @@ -68,19 +90,18 @@ function love.load() --font = love.graphics.newFont("DejaVuSansMono.ttf", fs) --love.graphics.setFont(font) + setmetatable(input, {__index = index}) - local lines = #input * precision - local seg = 1/lines - - for n=1, lines+1 do - local x = (n-1)*seg - local hue = (n-1)/lines - - local y = interpolate(hue, input, lerp) + input.interpolator = lerp + for x=win.L, win.R, xPrecision do + local y = input[x] addPoint(coordsA, x, y) addPoint(coordsAL, x, math.log(y)) + end - y = interpolate(hue, input, blendExp) + input.interpolator = blendExp + for x=win.L, win.R, xPrecision do + local y = input[x] addPoint(coordsB, x, y) addPoint(coordsBL, x, math.log(y)) end