Page 1 of 1

Recursive circle paths

Posted: Sun Jul 10, 2022 12:25 pm
by threedslider
Hello I would like to share you some my code in Purebasic based on the French book from algorithm in C programming :)

Here is the code :

Code: Select all

; Based on book : Algorithmes et structures de données by Michel Divay (C programming)
; Coded and adapted by threedslider 08.07.2022

Procedure Cercle(X.i, Y.i, R.i)
  For n=0 To 1800
        cx = R * Cos(n)
        cy= R * Sin(n)
        Plot(cx+X,cy+Y,RGB(255,0,0)) 
      Next
EndProcedure

Procedure DeuxCercles(X.i, Y.i, R.i)
  _r.i = 0
  If R > 10
      cercle(X, Y, R)    
      _r = R / 2
      DeuxCercles(X+_r, Y, _r)
      DeuxCercles(X-_r, Y, _r)
    EndIf       
EndProcedure
  
If OpenWindow(0, 0, 0, 600, 600, "Two circles in recursive", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
    If CreateImage(0, 600, 600) And StartDrawing(ImageOutput(0))
      
      DeuxCercles(300,300,200)
    
      StopDrawing() 
      ImageGadget(0, 0, 0, 200, 200, ImageID(0))
    EndIf
    
    Repeat
      Event = WaitWindowEvent()
    Until Event = #PB_Event_CloseWindow
EndIf

Re: Recursive circle paths

Posted: Sun Jul 10, 2022 12:28 pm
by threedslider
Same here but in three recursive circle paths.

Code: Select all

; Based on book : Algorithmes et structures de données by Michel Divay (C programming)
; Coded and adapted by threedslider 08.07.2022

Procedure Cercle(X.i, Y.i, R.i)
  For n=0 To 1800
        cx = R * Cos(n)
        cy = R * Sin(n)
        Plot(cx+X,cy+Y,RGB(255,0,0)) 
      Next
EndProcedure

Procedure TroisCercles(X.i, Y.i, R.i)
  _r.i = 0
  If R > 10
      cercle(X, Y, R)
      _r = Int((2*Sqr(3.0)-3)*R)
      H = R-_r
      TroisCercles(X-H, Y, _r)
      TroisCercles(X+H/2, Y+_r, _r)
      TroisCercles(X+H/2, Y-_r, _r)
    EndIf       
EndProcedure
  
If OpenWindow(0, 0, 0, 600, 600, "Three circles in recursive", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
    If CreateImage(0, 600, 600) And StartDrawing(ImageOutput(0))
      
      TroisCercles(300,300,200)
    
      StopDrawing() 
      ImageGadget(0, 0, 0, 200, 200, ImageID(0))
    EndIf
    
    Repeat
      Event = WaitWindowEvent()
    Until Event = #PB_Event_CloseWindow
EndIf