Growing cells sim

Applications, Games, Tools, User libs and useful stuff coded in PureBasic
User avatar
eddy
Addict
Addict
Posts: 1479
Joined: Mon May 26, 2003 3:07 pm
Location: Nantes

Growing cells sim

Post by eddy »

Hi,
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
Imagewin10 x64 5.72 | IDE | PB plugin | Tools | Sprite | JSON | visual tool
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: Growing cells sim

Post by IdeasVacuum »

That works really nicely, good code. 8)
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
Post Reply