Hello world ! In this first code poem we're going to be looking at the patterns in corals, brains and intestines. I had originally planned to illustrate this with a lot more pictures than actually I did but I didn't want this article to look like a 90's gore website, so we're mostly going to focus on the mathematics and pretty patterns found in coral reefs.

What we have here is a** Differential Growth (DG)** algorithm in its 2D variation often called Differential Line Growth. DG explores how a line folds on itself following simple motion rules and subdivisions patterns ressembling cellular division on a larger scale. The resulting forms closely ressemble known undulating phenomenons in the natural world such as corals, and that bloody stuff inside your skull and stomach.

The self-folding quality of any body that differential growth patterns induce allows them to efficiently extend themselves with a maximum surface inside a minimal volume. For a living being this means to be more in contact with your environment even in constrained spaces. For living creatures such as the Brain Coral of the Mussidae family this type of grooved surface allows them to grip on drifting animals and algae, making it easier for them to feed on.

## the differential line growth algorithm

My first approach followed the algorithm described by Jason Webb on the DG section of his Morphogenesis Resources page, though I found that the algorithm could be simplified and stripped down to its most basic functions whilst still giving the same output (I'll expand on this in the next section). Here is my proposition for a stripped down DG algorithm :

The system is initiated with **Step 1 : Create Nodes** in a setup function. **Step 2 : Adaptative Subdivision** and **Step 3 : Replusion Force** are then repeated at every frame of the simulation (typically in a draw function). Here is an example where you can see the new nodes popping up every frame :

The adaptative subdivision is relatively simple, it can be done by defining an **edge break** value, which will be the maximum distance between two nodes before creating a subdivision between them.

## differential forces and exponential repulsion

In Differential Growth what we call the differential forces are an addition of three forces, not unsimilar to boids, called **attraction**, **alignement** and **repulsion**. Though in the algorithm that I presented I only present a repulsion force because I believe that scaling it exponentially allows us to strip down the algorithm. Here's a comparison explaining the difference between a naive repulsion force and its exponential counterpart.

While a distance-based influence cutoff implementation can lead to an economy of calculations through the uses of data structures such as quadtrees, I found that using the exponential implementation keeps the nodes in perpetual movement (even if minimal) while balancing the system accurately. Here's what it looks like in pseudo code :

```
function repulsion(node) { // calculate repulsion for current node
let seek = new Vector();
for (let other of nodes) {
if (node != other) { // don't test against self
let distance = dist(node.pos, other.pos);
let diff = nodes.pos - other.pos;
diff *= exp(edgeBreak - distance); // invertly proportional
seek = diff - node.vel // seek = desired - velocity
seek += diff;
}
}
return seek;
}
```

From one folded brain to another, I'm really thrilled about finally sharing this on the internet and can't wait to show you the 3D variations based on the same algorithms that describe how flower petals fold. Ttyl !

**kaspar.ravel @ gmail dot com**.

This code was developped in javascript using p5.js, three.js and <3.

All is licensed as

**CC BY-SA 4.0**(Attribution-ShareAlike) meaning you are allowed to copy, share, remix, build upon, as long as you give appropriate credit and reshare with the same license.

- https://github.com/jasonwebb/morphogenesis-resources#diffusion-limited-aggregation-dla
- https://inconvergent.net/generative/differential-line/
- https://n-e-r-v-o-u-s.com/blog/?p=6721
- https://entagma.com/differential-line-growth/
- http://www.codeplastic.com/2017/07/22/differential-line-growth-with-processing/