PocketCHIPのPICO-8でボロノイ図を描画するプログラムを作ってみました。

イメージ 1



プログラムはたったのこれだけです。
ショートプログラムは俳句や短歌のようで楽しいです。

-- voronoi diagram
-- (c) time tripper 2016

bit={}
bn=20
bs=32
bc=9
bf=9

function _init()
for i=1,bn do
make_bit()
end
end

function make_bit()
local b={}
b.x=rnd(128)
b.y=rnd(128)
b.c=1+flr(rnd(15))
b.dx=2-rnd(4)
b.dy=2-rnd(4)
add(bit,b)
end

function move_bit(sp)
sp.x += sp.dx
sp.y += sp.dy
if sp.x < 0 then sp.dx *= -1 end
if sp.y < 0 then sp.dy *= -1 end
if sp.x > 127 then sp.dx *= -1 end
if sp.y > 127 then sp.dy *= -1 end
end

function _update()
if btn(0) and bc>1 then bc-=1 end
if btn(1) and bc<32 then bc+=1 end
if btn(2) and bf>0 then bf-=1 end
if btn(3) and bf<15 then bf+=1 end
if btn(4) then make_bit() end
if btn(5) then del(bit,bit[1]) end
foreach(bit,move_bit)
end

function _draw()
cls()
for i=bs,1,-1 do
for b in all(bit) do
circfill(b.x,b.y,i,(b.c+bf+flr(i/bc))%16)
end
end
print("count="..count(bit),0,0,8)
print("pattern="..bc,0,6,8)
print("color="..bf,0,12,8)
end