Fix infinite loop bug, add FFI enum test, add moving camera

This commit is contained in:
wan-may 2023-10-07 16:38:00 -03:00
parent d6b615c2c1
commit c24f86500e
3 changed files with 86 additions and 38 deletions

View File

@ -1,4 +1,27 @@
local enum = require 'enumeration' local enum = require 'enumeration'
local ffi = require 'ffi'
local function NadFFI( p )
local rmt = {
__lt = function(a, b) return a.n / a.d < b.n / b.d end,
__eq = function(a, b) return a.n == b.n and a.d == b.d end,
__call = function(t) return t.n / t.d end,
__tostring = function(t) return string.format( "<%d/%d>", t.n, t.d ) end
}
ffi.cdef "typedef struct { uint32_t n, d; } rational;"
ffi.metatype( "rational", rmt )
local list = ffi.new( "rational[?]", 123456 )
local d, i = 1, 1
repeat
d = d * p
for n = d - 1, 1, -p do
list[i].n = n
list[i].d = d
i = i + 1
end
until i > 123456 / p
return list
end
local function CalkinWilf() local function CalkinWilf()
local n, d = 1, 1 local n, d = 1, 1
@ -79,8 +102,19 @@ local function Dyad()
end ) end )
end end
local function Nad( p )
local n, d = -1, p
return enum( function()
n = n + p
if n > d then n, d = 1, d * p end
return n / d
end )
end
return { return {
Dyad = Dyad, Dyad = Dyad,
NadFFI = NadFFI,
Nad = Nad,
SternBrocot = SternBrocot, SternBrocot = SternBrocot,
CalkinWilf = CalkinWilf, CalkinWilf = CalkinWilf,
Random = Random, Random = Random,

View File

@ -1,8 +1,8 @@
local enumerations = require 'enumerations' local enumerations = require 'enumerations'
local twintree = require 'twintree' local twintree = require 'twintree'
local step = coroutine.wrap( twintree.buildIncremental ) local step = coroutine.wrap( twintree.buildIncremental )
local a = enumerations.Dyad() local a = enumerations.NadFFI( 2 )
local b = enumerations.Random() local b = enumerations.SternBrocot()
step( a, b ) step( a, b )
--Use LOVE for plotting. --Use LOVE for plotting.
@ -11,36 +11,28 @@ local love = assert( love )
local canvas = love.graphics.newCanvas() local canvas = love.graphics.newCanvas()
local numMapped, pointList = 0 local numMapped, pointList = 0
local function swap( i, j )
b[i], b[j] = b[j], b[i]
end
local function reset()
print( "changing coroutine" )
step = coroutine.wrap( twintree.buildIncremental )
print( "swapping" )
for i = 1, 10000 do
swap( i, math.random( 1, 10000 ) )
end
pointList = nil
numMapped = 0
print( "step" )
step( a, b )
end
local function new() local function new()
pointList = step() pointList = step()
numMapped = numMapped + 1 numMapped = #pointList / 2
end
local cx, cy = 0.0, 0.0
local zoomFactor = 1
local function zoom( out )
cx = cx + 0.5 * ( out - 1 ) / ( out * zoomFactor )
cy = cy + 0.5 * ( out - 1 ) / ( out * zoomFactor )
zoomFactor = zoomFactor * out
love.graphics.setLineWidth( 0.005 / zoomFactor )
end end
local function paint() local function paint()
if not pointList then return end
local type = type local type = type
--Make sure the whole point list consists of numbers! --Make sure the whole point list consists of numbers!
--We use the __call metamethod in non-number elements to coerce to numbers. --We use the __call metamethod in non-number elements to coerce to numbers.
--TODO: make this more generic, maybe amenable to FFI types. --TODO: make this more generic, maybe amenable to FFI types.
for i = 1, #pointList do for i = 1, #pointList do
if type( pointList[i] ) ~= 'number' then pointList[i] = pointList[i]() end if type( pointList[i] ) ~= 'number' then pointList[i] = pointList[i]( pointList[i] ) end
end end
--print( "Points: " ) --print( "Points: " )
@ -50,30 +42,34 @@ local function paint()
love.graphics.setCanvas( canvas ) love.graphics.setCanvas( canvas )
love.graphics.clear() love.graphics.clear()
love.graphics.setColor( 1, 1, 1, 0.5 ) love.graphics.print( ("%d\n%4.2f\n%4.2f\n%4.2f"):format( numMapped, cx, cy, zoomFactor ) )
love.graphics.print( numMapped )
love.graphics.push() love.graphics.push()
love.graphics.scale( love.graphics.getWidth(), love.graphics.getHeight() ) love.graphics.scale( love.graphics.getWidth(), love.graphics.getHeight() )
love.graphics.translate( 0, 1 ) love.graphics.translate( 0, 1 )
love.graphics.scale( 1, -1 ) love.graphics.scale( 1, -1 )
love.graphics.points( pointList ) love.graphics.scale( zoomFactor, zoomFactor )
love.graphics.translate( -cx, -cy )
love.graphics.setColor( 1, 1, 1, 0.3 )
love.graphics.line( 0,0, 1,0 )
love.graphics.line( 0,0, 0,1 )
love.graphics.line( 0, 0, 1, 1 )
love.graphics.line( pointList ) love.graphics.line( pointList )
love.graphics.setColor( 1, 0, 0, 0.1 * zoomFactor )
love.graphics.points( pointList )
love.graphics.pop() love.graphics.pop()
love.graphics.setCanvas() love.graphics.setCanvas()
end end
function love.wheelmoved() function love.wheelmoved( x, y )
for i = 1, 3 do new() end zoom( ( y > 0 ) and 1.01 or 0.99 )
paint() paint()
--[[ for i = 1, #sb do
print( i, sb[i], dy[i] )
end]]
end end
function love.mousepressed() function love.mousepressed()
--[[for i = 1, #sb do --pointList = twintree.flat( twintree.build( 10000, a, b ) )
print( i, sb[i] ) --numMapped = #pointList / 2
end]] paint()
end end
function love.draw() function love.draw()
@ -82,11 +78,27 @@ function love.draw()
love.graphics.draw( canvas ) love.graphics.draw( canvas )
end end
function love.keypressed() function love.update( dt )
print( "pressed key!" ) local repaint = false
return reset() and love.wheelmoved() if love.keyboard.isScancodeDown "r" then repaint = true; new() end
if love.keyboard.isScancodeDown "a" then repaint = true; cx = cx - dt * 0.1 / math.sqrt( zoomFactor ) end
if love.keyboard.isScancodeDown "d" then repaint = true; cx = cx + dt * 0.1 / math.sqrt( zoomFactor ) end
if love.keyboard.isScancodeDown "w" then repaint = true; cy = cy + dt * 0.1 / math.sqrt( zoomFactor ) end
if love.keyboard.isScancodeDown "s" then repaint = true; cy = cy - dt * 0.1 / math.sqrt( zoomFactor ) end
if love.keyboard.isScancodeDown "q" then repaint = true; zoom( 1 + dt ) end
if love.keyboard.isScancodeDown "e" then repaint = true; zoom( 1 - dt ) end
if repaint then return paint() end
end
function love.keypressed( key, code, isrepeat)
return paint()
end end
function love.load() function love.load()
love.graphics.setLineWidth( 0.001 ) --pointList = twintree.flat( twintree.build( 10000, a, b ) )
--numMapped = #pointList / 2
new()
love.graphics.setPointSize( 3 )
love.graphics.setLineWidth( 0.005 )
paint()
end end

View File

@ -46,6 +46,8 @@ local function insert( aTree, aSeq, aVals, bTree, bSeq, bVals )
newCandidate = true newCandidate = true
end end
end end
--print( "Inserting element in tree:", aIdx, aVal, bIdx, bVal )
--Insert elements into tree. --Insert elements into tree.
aTree[treeIdx], bTree[treeIdx] = aVal, bVal aTree[treeIdx], bTree[treeIdx] = aVal, bVal