Page 1 sur 1

Mandelbrot

Publié : jeu. 19/févr./2026 17:33
par threedslider
Sujet que je voulais faire approximativement le Fractal de Mandelbrot : https://fr.wikipedia.org/wiki/Ensemble_de_Mandelbrot

Basé sur le code en anglais : https://en.wikipedia.org/wiki/Mandelbrot_set

Voici le code :

Code : Tout sélectionner

;;; 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 !

;::: KNOW ISSUE :::
; - 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
Amusez vous bien ! :wink:

Re: Mandelbrot

Publié : jeu. 19/févr./2026 19:59
par venom
Merci du partage threedslider je testerai a l'occasion :wink:






@++

Re: Mandelbrot

Publié : ven. 20/févr./2026 9:32
par Jacobus
C'est toujours de superbes images générées, j'aime beaucoup. 8)
Dommage que ce soit si lent. Entre 20 et 30 secondes pour voir l'action de la souris 8O

Image
Image
Image
Image

Re: Mandelbrot

Publié : sam. 21/févr./2026 0:29
par Guillot
enleve le debogueur
et compile en c avec optimaisation

Re: Mandelbrot

Publié : sam. 21/févr./2026 9:50
par Jacobus
Merci Guillot, ça fonctionne effectivement beaucoup mieux ainsi. :D