This commit is contained in:
commit
5456e17c8b
1 changed files with 144 additions and 0 deletions
144
main.lua
Normal file
144
main.lua
Normal file
|
@ -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
|
Loading…
Reference in a new issue