My code is an approximation to Fractal but not perfect, not fully understand
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


