Hi,
I’m trying to make a Cell Division and Growth algorithm where cell agents are pushed right next to neighboring cell just after the division process. I’d like the final output to look like a circle packing.
However in my example sketch cells are pushed farther than their neighboring cell’s radii
How can I correct this behavior ?
def setup():
global collection
size(480, 360, P2D)
smooth(8)
collection = [Cell(PVector(width>>1, height>>1), 20)]
def draw():
background(255)
for c in collection:
c.growth()
c.interact()
c.display()
class Cell(object):
growthInterval = 10
divisionInterval = 100
def __init__(self, pos, rad):
self.position = pos
self.radius = rad
self.time = 0
def interact(self):
for a in collection:
if a is not self:
d = PVector.dist(a.position, self.position)
if d < a.radius + self.radius:
#gap = (a.radius + self.radius) - d
diff = PVector.sub(self.position, a.position)
diff.normalize()
diff.div(d)
a.position.sub(diff)
def growth(self):
self.time += 1
if len(collection) < 100:
if self.time > 0 and self.time%Cell.divisionInterval == 0:
if random(1) > .5:
dir = PVector.random2D()
self.radius *= .5
child = Cell(self.position+dir, self.radius)
collection.append(child)
self.position.sub(dir)
if self.time%Cell.growthInterval == 0:
self.radius += .5
def display(self):
strokeWeight(3)
point(self.position.x, self.position.y)
noFill()
strokeWeight(1)
ellipse(self.position.x, self.position.y, self.radius, self.radius)