This is a mini app to simulate growing cells
Code: Select all
Structure Dot
x.i
y.i
ClosedDir.i
direction.i
EndStructure
Structure Direction
x.i
y.i
EndStructure
Global Seed=2+Random(10000000)
Global NewList Dots.Dot()
Global Dim Directions.Direction(8)
For dy=-1 To 1
For dx=-1 To 1
Directions(dir)\x=dx
Directions(dir)\y=dy
dir+1
Next
Next
Enumeration
#canvas
EndEnumeration
Procedure NextDot(w, h)
Repeat
Protected ClosedDir=0
Protected CurrentIndex=ListIndex(Dots())
SelectElement(Dots(), ListSize(Dots())/(ListIndex(Dots())% 50+2))
Protected dirStart=CurrentIndex+Dots()\direction+Seed
For dirIndex=0 To ArraySize(Directions())
Protected dir=(dirStart+dirIndex) % (ArraySize(Directions())+1)
x=Dots()\x+Directions(dir)\x
y=Dots()\y+Directions(dir)\y
If x<0 Or x>=w Or y<0 Or y>=h Or Point(x, y)
ClosedDir+1
Continue
EndIf
AddElement(Dots())
With Dots()
\x=x
\y=y
\ClosedDir=ClosedDir
\direction=dir
EndWith
ProcedureReturn #True
Next
If ClosedDir=ArraySize(Directions())+1
DeleteElement(Dots())
EndIf
Until ListSize(Dots())=0
ProcedureReturn #False
EndProcedure
Procedure AddDot(init=0)
If StartDrawing(CanvasOutput(#canvas))
w=OutputWidth()
h=OutputHeight()
If init
Box(0, 0, w, h, $000000)
With Dots()
AddElement(Dots())
\x=w/2+i
\y=h/2+j
Plot(\x, \y, $FF)
EndWith
ElseIf ListSize(Dots())
If NextDot(w, h)
With Dots()
Protected k=255*\ClosedDir/8
Protected c=AlphaBlend(RGBA(255, 255, 0, 255-k), RGBA(0, 255, 0, k))
Plot(\x, \y, RGB(Red(c), Green(c), Blue(c)))
EndWith
EndIf
EndIf
StopDrawing()
EndIf
EndProcedure
If OpenWindow(0, 0, 0, 512, 512, "Growing", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
CanvasGadget(#canvas, 0, 0, WindowWidth(0), WindowHeight(0))
AddDot(1)
Repeat
AddDot()
SetWindowTitle(0, "Border Cells: "+ListSize(Dots()))
Until WaitWindowEvent(0)=#PB_Event_CloseWindow
EndIf