Polyominoes One More Time
It is of course necessary to display yesterday’s polyominoes, for our viewing pleasure.
I want to display some of these babies, maybe the pentominoes, since they are well known to all the people who know them. And I’m thinking we may be able to get to some better code, if the fancy strikes us.
I think the big design question will be how many we can draw across the screen. Doesn’t sound too daunting.
We’ll need to generate the pentomino collection in setup. I already have code, in a test, that will do that, but I think we can do better. Let’s just go for it.
After only a bit of moderate hackery, we have this:
function setup()
local mon = Omino()
mon:add(Sq(0,0))
local mons = OminoSet()
mons:add(mon)
result = mons
for i = 2,5 do
result = result:extend()
end
end
function draw()
local x,y
local size = 20
local order = 5
local rowSize = 9
local rowGap = 15
local colGap = 10
for i,omino in ipairs(result.coll) do
local pos = i-1
x = (pos%rowSize)*(order*size+colGap)
y = HEIGHT - (pos//rowSize + 1)*(order*size+rowGap)
pushMatrix()
translate(x,y)
omino:draw(size)
popMatrix()
end
end
That’s supported by this:
function Omino:draw(size)
pushStyle()
stroke(0)
strokeWidth(2)
fill(0,255,0)
for key, sq in pairs(self.squares) do
pushMatrix()
translate(sq.x*size, sq.y*size)
rect(0,0,size,size)
popMatrix()
end
popStyle()
end
And the result on screen is this:
Perfect. 63 of them, and it’s obvious that they are all unique and none are missing. For values of obvious.
That’ll do for now. I just wanted to know if I could do it and whether it would be “nice”. It’s not great, but it’s nice in the sense I intended, without special checks for anything in particular, just a general check for order having increased or not.