Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
QB64PE Mandelbrot
#1
Code: (Select All)
_Title "Mandelbrot Set in QB64PE"
Screen 12
Dim Shared xCenter As Double, yCenter As Double, scale As Double, maxIter As Long
xCenter = -0.5: yCenter = 0: scale = 2: maxIter = 100

Do
    Cls
    DrawMandelbrot xCenter, yCenter, scale, maxIter
    _Limit 120
    Select Case _KeyHit
        Case 18432: yCenter = yCenter - 0.1 * scale
        Case 20480: yCenter = yCenter + 0.1 * scale
        Case 19200: xCenter = xCenter - 0.1 * scale
        Case 19712: xCenter = xCenter + 0.1 * scale
        Case 61, 43: scale = scale / 1.1 '= or +
        Case 45, 95: scale = scale * 1.1 '- or _
        Case 113, 81: maxIter = maxIter + 10 'q      or Q
        Case 69, 101: maxIter = maxIter - 10 'e or E
        Case 27: System 'ESC
    End Select
    _Display
Loop

Sub DrawMandelbrot (xCenter As Double, yCenter As Double, scale As Double, maxIter As Long)
    Dim As Long x, y
    Dim As Double zx, zy, zx2, zy2, cx, cy
    Dim As Long iter
    For y = 0 To 479
        For x = 0 To 639
            cx = (x / 320 - 1.5) * scale + xCenter
            cy = (y / 240 - 1.0) * scale + yCenter
            zx = 0: zy = 0: zx2 = 0: zy2 = 0: iter = 0
            Do While (zx2 + zy2 <= 4) And (iter < maxIter)
                zy = 2 * zx * zy + cy
                zx = zx2 - zy2 + cx
                zx2 = zx * zx
                zy2 = zy * zy
                iter = iter + 1
            Loop
            If iter = maxIter Then
                PSet (x, y), 0
            Else
                PSet (x, y), iter * 255 / maxIter
            End If
        Next x
    Next y
End Sub

Simple enough to run/test.

Arrow Keys - Pan up / down / left / right.
+ / - - Zoom in / out.
Q / E - Increase / decrease maximum iterations.
Reply
#2
Very cool!!!! Good job.
Reply




Users browsing this thread: 1 Guest(s)