Mandelbrot

The Mandelbrot visualisation is one of the most fascinating visualisations on the website, simply due to the intriguing nature of the Mandelbrot itself. Visualised using the Mandelbrot set, each individual pixel is generated according to the set rules.

In creating the Mandelbrot, mathematical guidelines needed to be adhered to in order for the visualisation to be created correctly. The Mandelbrot set works by iterating through each pixel, using each individual pixel as a point in the complex plane. Iterating from 0, the visualisation uses the equation, z^2 + c, where z is the iterant and c is the point on the complex plane, until the resultant visualisation approaches infinity. Programmed using VB.Net, it also created a base for a class, which would also be used for the Filled Julia set and the Buddhabrot.

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Module MandelbrotSet
    Sub Main()
        Using BMP As New Drawing.Bitmap(1000, 1000), bmpgraphics = Drawing.Graphics.FromImage(BMP)
            For i = 0 To 1000 - 1
                For j = 0 To 1000 - 1
                    'Range Of Values
                    Dim Real As Double = 0.00000147602 * i - 0.74291189
                    Dim Complex As Double = 0.00000147602 * j + 0.13262005

                    Dim Properties As Escape = Escapes(New ComplexNumber(Real, Complex))
                    'Draw if it has escaped
                    If Properties.Escaped = True Then BMP.SetPixel(i, j, New HSBColour(0.36 * Properties.Iterations, 0.7, 0.7).HSBToRGB)
                Next
            Next

            BMP.Save("Mandelbrot.bmp", Drawing.Imaging.ImageFormat.Bmp)
        End Using
    End Sub

    ' z^2 + c
    Function Escapes(C As ComplexNumber) As Escape
        ' Start at 0
        Dim Current As New ComplexNumber(0, 0)
        For iterations = 0 To 1000
            Current = Current.Squared.Add(C)
            If Current.Modulus > 4 Then Return New Escape(True, iterations)
        Next
        Return (New Escape(False, -1))
    End Function
End Module

Class Escape
    Public Property Escaped As Boolean = False
    Public Property Iterations As Integer
    Sub New(E As Boolean, I As Integer)
        Escaped = E
        Iterations = I
    End Sub
End Class

Class ComplexNumber
    Public Property Real As Double
    Public Property Complex As Double
    Sub New(R As Double, C As Double)
        Real = R
        Complex = C
    End Sub
    Function Squared() As ComplexNumber
        Dim RealTemp As Double = Real * Real - Complex * Complex
        Complex = 2 * Real * Complex
        Real = RealTemp
        Return New ComplexNumber(Real, Complex)
    End Function
    Function Modulus() As Double
        Return Real * Real + Complex * Complex
    End Function
    Function Add(Num As ComplexNumber) As ComplexNumber
        Return New ComplexNumber(Real + Num.Real, Complex + Num.Complex)
    End Function
End Class

References

https://en.wikipedia.org/wiki/Mandelbrot_set