Mandelbrot

Share your advanced PureBasic knowledge/code with the community.
threedslider
Enthusiast
Enthusiast
Posts: 558
Joined: Sat Feb 12, 2022 7:15 pm

Mandelbrot

Post by threedslider »

I was inspired by Mandelbrot for his Fractal, see the wikipedia with more info : https://en.wikipedia.org/wiki/Mandelbrot_set

My code is an approximation to Fractal but not perfect, not fully understand :shock:

Here my code :

Code: Select all

;;; Inspired by Mandelbrot with Fractal and done with appromixation by threedslider in Purebasic 6.30 - 21/01/2026  ;;;

; Click ans select a region you want to zoom as infinitely, enjoy :D !

;::: ISSUE KNOW :::
; - Some in region doesn't look very well as true Fractal, sorry !


EnableExplicit

#W = 800
#H = 600
#MAXITER = 300

Global minX.d = -2.5
Global maxX.d =  1.0
Global minY.d = -1.5
Global maxY.d =  1.5

Global selecting = #False
Global sx, sy, ex, ey

Procedure.d MinD(a.d, b.d)
  If a < b : ProcedureReturn a : Else : ProcedureReturn b : EndIf
EndProcedure

Procedure.d MaxD(a.d, b.d)
  If a > b : ProcedureReturn a : Else : ProcedureReturn b : EndIf
EndProcedure

Procedure DrawMandelbrot()
  Protected x, y, i
  Protected zx.d, zy.d, cx.d, cy.d
  Protected dx.d = (maxX - minX) / #W
  Protected dy.d = (maxY - minY) / #H

  StartDrawing(CanvasOutput(0))
  Box(0,0,#W,#H,0)

  For y = 0 To #H-1
    cy = minY + y * dy
    For x = 0 To #W-1
      cx = minX + x * dx
      zx = 0 : zy = 0 : i = 0

      While zx*zx + zy*zy < 4 And i < #MAXITER
        Protected t.d = zx*zx - zy*zy + cx
        zy = 2*zx*zy + cy
        zx = t
        i + 1
      Wend

      If i < #MAXITER
        Plot(x, y, RGB(i*4, i*6, i*3))
      EndIf
    Next
  Next

  StopDrawing()
EndProcedure

; --------- UI ---------
OpenWindow(0, 0, 0, #W/DesktopResolutionX(), #H/DesktopResolutionY(), "Mandelbrot - Zoom souris", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
CanvasGadget(0, 0, 0, #W, #H)

DrawMandelbrot()

Repeat
  Select WaitWindowEvent()
    Case #PB_Event_CloseWindow
      Break

    Case #PB_Event_Gadget
      If EventGadget() = 0
        Select EventType()
          Case #PB_EventType_LeftButtonDown
            sx = GetGadgetAttribute(0, #PB_Canvas_MouseX)
            sy = GetGadgetAttribute(0, #PB_Canvas_MouseY)
            selecting = #True

          Case #PB_EventType_MouseMove
            If selecting
              ex = GetGadgetAttribute(0, #PB_Canvas_MouseX)
              ey = GetGadgetAttribute(0, #PB_Canvas_MouseY)

              DrawMandelbrot()
              StartDrawing(CanvasOutput(0))
              DrawingMode(#PB_2DDrawing_Outlined)
              Box(sx, sy, ex-sx, ey-sy, RGB(255,255,255))
              StopDrawing()
            EndIf

          Case #PB_EventType_LeftButtonUp
            If selecting
              selecting = #False
              ex = GetGadgetAttribute(0, #PB_Canvas_MouseX)
              ey = GetGadgetAttribute(0, #PB_Canvas_MouseY)

              If Abs(ex-sx) > 10 And Abs(ey-sy) > 10
                Define nx1.d = minX + (sx/#W)*(maxX-minX)
                Define nx2.d = minX + (ex/#W)*(maxX-minX)
                Define ny1.d = minY + (sy/#H)*(maxY-minY)
                Define ny2.d = minY + (ey/#H)*(maxY-minY)

                minX = MinD(nx1,nx2)
                maxX = MaxD(nx1,nx2)
                minY = MinD(ny1,ny2)
                maxY = MaxD(ny1,ny2)

                DrawMandelbrot()
              EndIf
            EndIf
        EndSelect
      EndIf
  EndSelect
ForEver
User avatar
STARGÅTE
Addict
Addict
Posts: 2280
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: Mandelbrot

Post by STARGÅTE »

PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
threedslider
Enthusiast
Enthusiast
Posts: 558
Joined: Sat Feb 12, 2022 7:15 pm

Re: Mandelbrot

Post by threedslider »

Wow ! It works now with PB 6.30 !! Your Mandelbrot is very fast and more stable than mine :shock:

Thanks for inspiration :mrgreen:
User avatar
mk-soft
Always Here
Always Here
Posts: 6514
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Mandelbrot

Post by mk-soft »

My first PC calculated over 8 hours (PC-XT 8 Mega Hz with Hercules graphics card) 8)
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
Post Reply