# Modelling Percolation

Based on an article in Scientific American- April 2021.

The phenomenon of 'percolation' the passage of fluid or gas through solids of varying permeability, also has relevance to spread of diseases or polymerisation. The article stimulated me to animate the process as I found the article' static diagrams rather weak for helping understanding!

I create a rectangular grid representing nodes linked to their four neighbours ( or an edge). I then block these with a chosen probability- eg 0.5 means on average half the links are blocked. 100% is no-connections. 0% means 'no links obstructed'.

I then find in turn all entry links which are open, and follow each to the end of all connections that are open- or until it reaches the far-left side.

In the animation below, you can see how far the blue 'water' gets for ten different probabilities.

### LB code for one version

```    nomainwin

global offset
offset       = 50
Rand         =  0.6    '   probability crack is open

WindowWidth  =640
WindowHeight =630

open "Percolation" for graphics_nsb as #wg

hw      =hwnd( #wg)
calldll #user32, "GetDC", hw as ulong, hdc as ulong

#wg "trapclose quit"

for Rand =0.00 to 1.00 step 0.1
#wg "cls ; down"
#wg "backcolor 255 0 0 ; fill 192 192 192"
call delay 100
#wg "place "; offset; " "; 0
#wg "boxfilled "; offset +610; " "; 610

call makeGrid

#wg "size 10 ; color black ; down ; set 620 300"
'targetcolor   =192 +192 *2^8 +192 *2^16
targetcolor   =int( 255 +0 *2^8 +0 *2^16)   '   to fill out to or within. Here RED.
x   =620:     y   =320  '   fill with blue water from near the black spot.

#wg "backcolor 150 150 255"

#wg "size 3"

call delay 1000

calldll #gdi32, "ExtFloodFill",_
hdc                as ulong,_
x                  as long,_
y                  as long,_
targetcolor        as long,_
_ '_FLOODFILLSURFACE  as  long, result as  long 'ie fill out 'WHILE this colour is met.
_FLOODFILLBORDER   as  long, result as  long 'ie fill out 'til this colour is met..

#wg "place 8 60 ; color red ; backcolor 192 192 192"
#wg "\"; using( "#.###", Rand)
#wg "flush ; getbmp scr 1 1 630 600"
bmpsave "scr", "rocks_" +str\$( Rand) +".bmp"

call delay 1000
next Rand

wait

sub makeGrid
#wg "color 192 192 192 ; size 3"
for x =offset +0 -10 to 610 step 10
#wg "line "; x; " "; 0; " "; x; " "; 600
next x

for y =0 -10 to 600 step 10
#wg "line "; offset; " "; y; " "; offset +560; " "; y
next y

#wg "backcolor 192 192 192 ; up ; goto 612 0 ; down ; boxfilled "; 630; " "; 630
#wg "backcolor 192 192 192 ; up ; goto   0 0 ; down ; boxfilled ";  50; " "; 630
scan
end sub

#wg "color 255 0 0"
#wg "size 10"
for x =offset +5 to offset +550 step 10
for y =0 to 600 step 10
if probability <=rnd( 1) then #wg "set "; x +5; " "; y
next y
next x
scan
end sub

sub delay sleeptime
calldll #kernel32, "Sleep", sleeptime as long, ret as void
scan
end sub

sub quit h\$
calldll #user32, "ReleaseDC", hw as ulong, hdc as ulong, ret as void   'release the DC
close #h\$
end
end sub

```

Diga Me!