# Studio/Gallery/DLA

The following is a simple attempt at implementing DLA. From the style of the code, looks like it was largely lifted from an implementation in a non-array-oriented language.

For an example of modifying this code to make it more J-like, look here and here.

Diffusion-Limited Aggregation

1) On a 2 Dimensional lattice place "seed" particles, usually a single point in the center of the lattice.

2) Release particles on the lattice, which will commence randomly walk the lattice until the particle either:

a) wanders outside of the bounds established and the particle is then disposed off OR.. b) bumps into an occupied point in the lattice, and sticks to become part of a growing cluster

3) And then release a new particle and repeat step (2)

A DLA cluster generated with J601 and rendered with viewmat.

Here is the J601 code I have been working with.

```load 'coutil'
cocurrent 'dla'

NB. State settings
size =: 0 NB.Keep track of the size of the cluster
nbs =: 1 1j1 0j1 _1j1 _1 _1j_1 0j_1 1j_1 NB. Neighbors to a point in complex numbers
space =: 500 500 \$ 0                     NB. Lattice to grow the cluster

NB. Operations and helpers
getCenter =: (j./) @ ( -: @ \$)         NB. Get the center of the lattice
seed =: 3 : '1 (< +. getCenter y) } y' NB. place a single point in the center of lattice
csign =: (+.^:_1) @ ( * @ +.)          NB. get the 'sign' of a complex number i.e 0.25j_0.5 -> 1j_1
walk =: 3 : 'y + (? # nbs) { nbs'      NB. Simple form of random walk
walk2 =: 3 : 'y + (? 1 +  # nbs) { nbs, csign center - y' NB. Baised toward center of lattice
NB. Used to calculate the max radius of the cluster
dist =: 4 : 0
d =:(0 { *. (x - y))
>.(size >. d )
)
NB. release a new particle distance x + 5 from center y
release =: 4 : 0
r =. (x + 5) r. (2p1 * ? 0)
<. r + y
)
space =: 1 (< +. y) } space
)
NB. This probably could be improved. Used to find if a point has landed on the cluster
NB. Look at the 8 neighbors around a point and see if there are any occupied cells.
range =: <:,],>:
drange =: (range@(0&{@+.)); (range@(1&{@+.))
check_collision =: 3 : '+/+/(< drange y) { space'
NB. Check to see if the point has wandered far away from the cluster.
outOfBounds =: 4 : '(size + 10) <(0 { *. (x - y))'

NB. Init
center =: getCenter space
space =: seed space

NB. Process
grow =: 3 : 0
c =. 0
point =: size release center
while. y > c do.
point =: walk point
if. point outOfBounds center do.
point =: size release center
else.
if. check_collision point do.
size =: center dist point
break.
end.
end.
c =. >: c
end.
)
```

And here is how I typically use the module in a J session:

```    load 'c:\j601\temp\cdla6.ijs'

grow_dla_"0 (1000#1000)
1 2 2 2 3 3 3 3 4 4 4 4 4 4 5 5 5 5 6 6 6 6 6 7 7 7 7 7 8 8 ...
NB. List of steps till point reached the cluster
+/ +/ space_dla_
1001 NB. Number of particles in the cluster
viewmat space_dla_
```