When I was a kid, I was obsessed with drawing binary combinations (0000, 0001, 0010, 0011, 0100, 0101...), both numerically and graphically. Today I did that again, but instead of only 2 digits (0 and 1), I did it with 4 letters (A, B, C and D). The result was VERY extensive, took the whole paper, but then it gave me the idea of coding this. It's a small contraption that lets you choose how many digits each combination will have (0001 is 2 digits, 0012 is 3 digits, 0123 is 4 digits, etc.), as well as the size of each combination (0000 is 4 spaces, 00000 is 5, etc.)
Code: Select all
function love.load()
love.window.setTitle("Probabilities/Combinations Experiment - HugoBDesigner")
windowW, windowH = 800, 200
love.graphics.setFont(love.graphics.newFont(20))
love.window.setMode(windowW, windowH)
matrix = {}
scrollH = {x = 10, y = windowH-15/2, width = windowW-10-10, height = 4, pos = 0, barwidth = (windowW-10-10)/2}
digits = 2
spaces = 4
scrolling = false
scrollFactor = 0
newMatrix()
end
function love.update(dt)
if scrolling and scrolling[1] == "h" then
local x = love.mouse.getX()
scrollH.pos = math.max(0, math.min(1, (x-scrolling[2]-scrollH.x)/(scrollH.width-scrollH.barwidth)))
end
end
function love.draw()
love.graphics.setLineWidth(1)
love.graphics.setColor(255, 255, 255, 255)
local xx, yy, ww, hh = 10, windowH-15-#matrix*5, math.min(windowW-10-10, string.len(matrix[1])*5), math.min(windowH/2-10, #matrix*5)
love.graphics.rectangle("line", xx-2, yy-2, ww+4, hh+4)
love.graphics.setColor(0, 50, 55, 255)
love.graphics.rectangle("fill", xx, yy, ww, hh)
love.graphics.setColor(255, 255, 255, 255)
love.graphics.line(3, windowH-15-15-8*5, windowW-3, windowH-15-15-8*5)
love.graphics.line(windowW-windowH, 3, windowW-windowH, windowH-15-15-8*5 - 4)
love.graphics.print("FPS: " .. love.timer.getFPS(), windowW-windowH+5, 5)
love.graphics.print("Render delay: " .. round(love.timer.getDelta()), windowW-windowH+5, 5+20)
local t = {
"Use < or > to increase/decrease the amount of digits.",
"Use ^ or v to increase/decrease the amount of lines.",
"",
"Amount of digits: " .. digits,
"Amount of lines: " .. spaces,
"Total amount of combinations: " .. digits^spaces
}
for i, v in ipairs(t) do
love.graphics.print(v, 5, 5+(i-1)*20)
end
love.graphics.push()
love.graphics.setScissor(xx, yy, ww, hh)
love.graphics.translate(xx-scrollFactor*scrollH.pos, yy)
for i, v in ipairs(matrix) do
for j = 1, string.len(v) do
local n = string.sub(v, j, j)
if n == "1" then
love.graphics.setColor(255, 255, 255, 255)
love.graphics.rectangle("line", (j-1)*5, (i-1)*5, 5, 5)
love.graphics.setColor(205, 205, 205, 255)
love.graphics.rectangle("fill", (j-1)*5, (i-1)*5, 5, 5)
end
end
end
love.graphics.setScissor()
love.graphics.pop()
local x, y = love.mouse.getPosition()
love.graphics.setLineWidth(2)
love.graphics.setColor(255, 255, 255, 55)
love.graphics.line(scrollH.x, scrollH.y+scrollH.height/2, scrollH.x+scrollH.width, scrollH.y+scrollH.height/2)
love.graphics.setColor(155, 155, 155, 255)
local v = scrollH
if inside(x, y, 0, 0, v.x+v.pos*(v.width-v.barwidth), v.y, v.barwidth, v.height) then
love.graphics.setColor(155, 205, 255, 255)
end
if scrolling and scrolling[1] == "h" then
love.graphics.setColor(0, 155, 255, 255)
end
love.graphics.rectangle("fill", v.x+v.pos*(v.width-v.barwidth), v.y, v.barwidth, v.height)
end
function love.mousepressed(x, y, button)
local v = scrollH
if button == "l" and inside(x, y, 0, 0, v.x+v.pos*(v.width-v.barwidth), v.y, v.barwidth, v.height) then
scrolling = {"h", x-(v.x+v.pos*(v.width-v.barwidth))}
elseif button == "wu" then
scrollH.pos = math.min(1, scrollH.pos+(scrollH.barwidth/2)/scrollH.width)
elseif button == "wd" then
scrollH.pos = math.max(0, scrollH.pos-(scrollH.barwidth/2)/scrollH.width)
end
end
function love.mousereleased(x, y, button)
if button == "l" then scrolling = false end
end
function love.keypressed(key)
local ns, nd = spaces, digits
if key == "up" or key == "w" or key == "=" then
spaces = math.min(8, spaces+1)
elseif key == "down" or key == "s" or key == "-" then
spaces = math.max(2, spaces-1)
elseif key == "left" or key == "a" or key == "," then
digits = math.max(2, digits-1)
elseif key == "right" or key == "d" or key == "." then
digits = math.min(6, digits+1)
end
if ns ~= spaces or nd ~= digits then
scrollH.pos = 0
newMatrix(digits, spaces)
end
end
function newMatrix(d, s)
matrix = {}
local d = d or 2
local s = s or 4
for i = 1, s do
table.insert(matrix, 1, "")
local n = 0
for j = 1, d^s do
n = n+1
if n > d^i then n = 1 end
if n <= d^i/d then
matrix[1] = matrix[1] .. "1"
else
matrix[1] = matrix[1] .. "0"
end
end
end
scrollH.width = math.min(windowW-10-10, d^s*5)
scrollH.barwidth = math.max( 20, scrollH.width/((d^s*5)/scrollH.width) )
if d^s*5 > windowW-10-10 then
scrollFactor = d^s*5 - (windowW-10-10)
end
end
function inside(x1, y1, w1, h1, x2, y2, w2, h2)
if x1 >= x2 and x1+w1 <= x2+w2 and y1 >= y2 and y1+h1 <= y2+h2 then
return true
end
return false
end
function round(n, d)
local d = d or 2
return math.floor(n*10^d)/10^d
end