This is a very basic implementation of a voroni cell system. I imagine is would be very useful for making things like crates that split apart when they hit something.
The entire “real” code is called by the function reset(). The rest is simply an interface for calling it and displaying it.
The subspecies is generates are all found as points tables in vpoints[i][3] where I is the cell number.
supportedOrientations(LANDSCAPE_ANY)
function setup()
iparameter("dimensions",7,44,17)
parameter("boxsize",5,HEIGHT*0.9,HEIGHT/1.5)
iparameter("cellnumber",3,11,4)
iparameter("seed",1,99999,math.random(1,99999))
--iparameter("check",1,29,3)
grid = {}
noSmooth()
reset()
end
--function(a,b) return a>b end
function orderpoints()
for k = 1,cells do
vs = vec2(1,9)
currentcell =vpoints[k][1]
table.sort(vpoints[k][3], function(a,b) return
(180 + vs:angleBetween(a-currentcell) < 180 +vs:angleBetween(b-currentcell))
end)
currentcell = nil
table.insert(
vpoints[k][3],
1 + #vpoints[k][3],
vpoints[k][3][1])
end
end
function cornercheck(xd,yd,colors)
local here = vec2(xd-1/2,yd-1/2)
local types = {}
local added = 0
for i = -2,cells do -- i is the slot number, in terms of which of
--the many cells it is part of.
for j = 1,#colors do
-- this is which of the four colors is being read
if colors[j] == i then
types[i] = here
end
end
end
for i = -2,cells do
if types[i] == here then
added = added + 1
end
end
if added >2 then
added = 0
there = vec2(0,0)
if here.x > 1 and here.x < dim and here.y > 1 and here.y < dim then
for i = 1,cells do
if types[i] == here then
there = there + vpoints[i][1]
added = added + 1
there = there*0.9 + here/10
end
end
there = there / added
else
there = here
end
for i = 1,cells do
if types[i] == here then
table.insert(vpoints[i][3],1,there)
end
end
end
end
function termiterun()
termite = 0
termite = {}
termitedir = {
vec2(-1,-1),
vec2(0,-1),
vec2(0,0),
vec2(-1,0)
}
for i = 1,dim+1 do
for j = 1,dim+1 do
termite = {0,0,0,0}
for k = 1,4 do
local where = vec2(i,j)+termitedir[k]
if where.x == 0 or where.x == dim+1 then
termite[k] = -2
end
if where.y == 0 or where.y == dim+1 then
termite[k] = -1
end
if termite[k] == 0 then
termite[k] = grid[where.x][where.y]
end
end
table.sort(termite)
tur = 0
tur =termite
cornercheck(i,j,tur)
-- end
end
end
end --end of termite function
function voronicalc(me)
local lowest = dim *5
local index = 1
for i = 1,cells do
local fine = me:dist(vpoints[i][1])
if fine < lowest then
index = i
lowest = fine
end
end
return index
end
function randchan()
return math.random(5,255)
end
function reset()
math.randomseed(seed)
vpoints = {}
cells = cellnumber
seedbuff = seed
dim = dimensions
for i = 1,cells do
vpoints[i] = {}
vpoints[i][1] = vec2(math.random(1,dim-1),math.random(1,dim-1))/2 +
vec2(math.random(1,dim-1),math.random(1,dim-1))/2 +
vec2(math.random(0,50),math.random(0,50))/50
vpoints[i][2] = color(randchan(), randchan(), randchan(), 240)
vpoints[i][3]={}
end
dim = dimensions
for i = 1,dim do
grid[i]= {}
for j = 1,dim do
grid[i][j] = voronicalc(vec2(i,j))
end
end
termiterun()
orderpoints()
end
function draw()
size = boxsize/dim
fontSize(size/2)
if seedbuff ==seed and cells == cellnumber and dim == dimensions then else reset() end
background(25, 25, 25, 255)
strokeWidth(0)
font("AmericanTypewriter-Bold")
fill(255, 255, 255, 255)
for i = 1,dim do
for j = 1,dim do
fill(vpoints[grid[i][j]][2])
text("•",i*size,j*size)
end
end
for i = 1,cells do
local pointy =vpoints[i]
fill(0, 0, 0, 255)
fontSize(size/1.2+19)
-- ellipse(pointy[1].x*size,pointy[1].y*size,size/1.2+9)
stroke(vpoints[i][2])
fill(255, 255, 255, 255)
fontSize(size/1.2)
strokeWidth(size/11)
text(i,pointy[1].x*size,pointy[1].y*size)
pointset = vpoints[i][3]
for j = 1, #pointset-1 do
vecline(pointset[j]*size,pointset[j+1]*size)
end
end
end
function vecline(aa,bb)
line(aa.x,aa.y,bb.x,bb.y)
end
Edit- missing set caused problem. Corrected.