Merge remote-tracking branch 'love_plotting/master'
This commit is contained in:
commit
213b5d0ccb
1 changed files with 165 additions and 0 deletions
165
main.lua
Normal file
165
main.lua
Normal file
|
@ -0,0 +1,165 @@
|
|||
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]=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 = 360,
|
||||
stepX = 30,
|
||||
stepY = 0.25,
|
||||
}
|
||||
win.scaleX = 1/(win.L - win.R)
|
||||
win.scaleY = 1/(win.T - win.B)
|
||||
|
||||
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 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()
|
||||
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)
|
||||
setmetatable(input, {__index = index})
|
||||
|
||||
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
|
||||
|
||||
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
|
||||
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