B4J Code Snippet [B4X] Mandelbrot Fractal

fractals.gif


This code uses BitmapCreator class to create a Mandelbrot fractal:
B4X:
Sub CreateMandelbrotFractal As B4XBitmap
   Dim w As Int = bc.mWidth
   Dim h As Int = bc.mHeight
   Dim tx = FractalRect.Width, ty = FractalRect.Height As Double
   For row = 0 To w - 1
       For col = 0 To h - 1
           Dim real As Double = col / w * tx + FractalRect.Left
           Dim img As Double = row / h * ty + FractalRect.Top
           Dim x = 0, y = 0 As Double
           Dim iterations As Int
           Do While x * x + y * y < 4 And iterations < MAX_ITERATIONS
               Dim x_new As Double = x * x - y * y + real
               y = 2 * x * y + img
               x = x_new
               iterations = iterations + 1
           Loop
           If iterations < MAX_ITERATIONS Then
               bc.SetHSV(col, row, iterations / MAX_ITERATIONS * 360, 1, iterations / (iterations + 8))
           Else
               bc.SetColor(col, row, xui.Color_Black)
           End If
       Next
   Next
   Return bc.Bitmap
End Sub

Example is attached.
 

Attachments

  • Fractal.zip
    3.7 KB · Views: 541
Top