Mandelbrot

Partagez votre expérience de PureBasic avec les autres utilisateurs.
Avatar de l’utilisateur
threedslider
Messages : 465
Inscription : dim. 01/juil./2018 22:38

Mandelbrot

Message 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:
Avatar de l’utilisateur
venom
Messages : 3183
Inscription : jeu. 29/juil./2004 16:33
Localisation : Klyntar
Contact :

Re: Mandelbrot

Message par venom »

Merci du partage threedslider je testerai a l'occasion :wink:






@++
Windows 10 x64, PureBasic 6.30 x86 & x64
GPU : radeon HD6370M, CPU : p6200 2.13Ghz
Avatar de l’utilisateur
Jacobus
Messages : 1612
Inscription : mar. 06/avr./2004 10:35
Contact :

Re: Mandelbrot

Message 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
Quand tous les glands seront tombés, les feuilles dispersées, la vigueur retombée... Dans la morne solitude, ancré au coeur de ses racines, c'est de sa force maturité qu'il renaîtra en pleine magnificence...Jacobus.
Avatar de l’utilisateur
Guillot
Messages : 742
Inscription : jeu. 25/juin/2015 16:18

Re: Mandelbrot

Message par Guillot »

enleve le debogueur
et compile en c avec optimaisation
Avatar de l’utilisateur
Jacobus
Messages : 1612
Inscription : mar. 06/avr./2004 10:35
Contact :

Re: Mandelbrot

Message par Jacobus »

Merci Guillot, ça fonctionne effectivement beaucoup mieux ainsi. :D
Quand tous les glands seront tombés, les feuilles dispersées, la vigueur retombée... Dans la morne solitude, ancré au coeur de ses racines, c'est de sa force maturité qu'il renaîtra en pleine magnificence...Jacobus.
Répondre