# Pushing cell agents

**URL:** https://discourse.processing.org/t/pushing-cell-agents/6077
**Category:** Processing.py
**Created:** [November 29, 2018, 7:10pm UTC](https://discourse.processing.org/t/pushing-cell-agents/6077 "2018-11-29T19:10:07Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![solub](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/solub/32/333_2.png) [@solub](https://discourse.processing.org/u/solub)
#### Post date: [November 29, 2018, 7:10pm UTC](https://discourse.processing.org/t/pushing-cell-agents/6077/1 "2018-11-29T19:10:08Z")

</div>

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.

![](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/4/415a5f3efae7a0d99ee5f027200e3f882b1fb35f.gif)

However in my example sketch cells are pushed farther than their neighboring cell’s radii

How can I correct this behavior ?

```auto
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)

```

---

<div class="post-metadata">

### Author: ![solub](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/solub/32/333_2.png) [@solub](https://discourse.processing.org/u/solub)
#### Post date: [November 29, 2018, 8:08pm UTC](https://discourse.processing.org/t/pushing-cell-agents/6077/2 "2018-11-29T20:08:10Z")

</div>

So it seems I have to divide the radii by 2 (on the fourth line of the “interact” function) but it doesn’t make sense because I already did it on line 7 in the “growth” function.

If someone can find what’s going on.

---

<div class="post-metadata">

### Author: ![Sarah](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/sarah/32/1675_2.png) [@Sarah](https://discourse.processing.org/u/Sarah)
#### Post date: [November 29, 2018, 10:25pm UTC](https://discourse.processing.org/t/pushing-cell-agents/6077/3 "2018-11-29T22:25:52Z")

</div>

Here:

```auto
diff = PVector.sub(self.position, a.position)
diff.normalize()
diff.div(d)
a.position.sub(diff)    

```

If you’re normalizing the vector, shouldn’t you multiply it by its radius instead? (Not divide it by the distance)

---

<div class="post-metadata">

### Author: ![solub](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/solub/32/333_2.png) [@solub](https://discourse.processing.org/u/solub)
#### Post date: [November 30, 2018, 11:57am UTC](https://discourse.processing.org/t/pushing-cell-agents/6077/4 "2018-11-30T11:57:37Z")

</div>

Hi Sarah,

I think that part (that I’m borrowing from Daniel Shiffman’s tutorial on boids) isn’t appropriate in this sketch and I shouldn’t be normalizing the the vectors’ distances in the first place. But you’re right in noting that I should _do something_ with the radii at some point (but don’t know what exactely).

Here’s what I came up with instead:

```auto

def interact(self):
    for a in collection:
        if a is not self:

            #Distance between 2 vectors
            d = PVector.dist(self.position, a.position)

            #If that distance is smaller than the sum of the radii (+ strokeWeight)
            if d < (a.radius*.5 + self.radius*.5 + 1):

                #Compute gap distance
                gap = (a.radius*.5 + self.radius*.5 + 1) - d

                #Not sure what I'm doing here
                diff = PVector.sub(self.position, a.position)

                #Pushing the cell to the right distance (setting the magnitude to the sum of the radii)
                a.position.sub(diff.setMag(gap))

```

![](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/9/9e3e791848f4fb7aee33b7d71eb4d3615feba424.gif)

Still, I don’t understand why I have to divide the radii by 2 again. It shouldn’t be that way.

---

<div class="post-metadata">

### Author: ![Sarah](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/sarah/32/1675_2.png) [@Sarah](https://discourse.processing.org/u/Sarah)
#### Post date: [November 30, 2018, 11:26pm UTC](https://discourse.processing.org/t/pushing-cell-agents/6077/5 "2018-11-30T23:26:19Z")

</div>

I think you might be mixing the radius with the diameter of the circle. The radius should be half of the circles diameter so instead of  
`ellipse(self.position.x, self.position.y, self.radius, self.radius)`  
it should be  
`ellipse(self.position.x, self.position.y, self.radius*2, self.radius*2)`

---

<div class="post-metadata">

### Author: ![solub](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/solub/32/333_2.png) [@solub](https://discourse.processing.org/u/solub)
#### Post date: [December 1, 2018, 1:42pm UTC](https://discourse.processing.org/t/pushing-cell-agents/6077/6 "2018-12-01T13:42:01Z")

</div>

Of course… thank you Sarah.
