Difference between revisions of "Mandelbrot set"

From ScienceZero
Jump to: navigation, search
Line 2: Line 2:
 
:Z = Z<sup>2</sup> + C
 
:Z = Z<sup>2</sup> + C
  
C is a point in the complex plane where i is on the vertical axis and real numbers are horizontal.
+
If Z escapes to infinity then we are outsidde the set, otherwise we are inside.
 +
 
 +
C is a point in the complex plane where i is on the vertical axis and real numbers are on the horizontal axis.
 
A complex number has the form of x+iy where x and y are real numbers and i is the sqare root of -1.
 
A complex number has the form of x+iy where x and y are real numbers and i is the sqare root of -1.
 
so the formula can also be written:
 
so the formula can also be written:

Revision as of 16:50, 31 January 2007

The formula for the mandelbrot set is:

Z = Z2 + C

If Z escapes to infinity then we are outsidde the set, otherwise we are inside.

C is a point in the complex plane where i is on the vertical axis and real numbers are on the horizontal axis. A complex number has the form of x+iy where x and y are real numbers and i is the sqare root of -1. so the formula can also be written:

(Zr+Zi) = ((Zr+Zi) * (Zr+Zi)) + (Cr+Ci)

Complex addition has the form:

(a+bi)+(c+di) = (a+c)+i(b+d)

Complex multiplication has the form:

(a+bi)*(c+di) = (ac-bd)+i(ad+bc)

so

(Zr+Zi * Zr+Zi) = (Zr*Zr-Zi*Zi)+i(Zr*Zi+Zi*Zr)

and

(Zr*Zr-Zi*Zi)+i(Zr*Zi+Zi*Zr) + (Cr+Ci) = (Zr*Zr-Zi*Zi)+Cr+i(Zr*Zi+Zi*Zr)+Ci

that gives us

Zr = (Zr*Zr-Zi*Zi)+Cr
Zi = (Zr*Zi+Zi*Zr)+Ci

we must remember to use temporary values to calculate Zr so it does not affect the result of Zi.

Z = C when we enter the loop. If Z escapes to infinity at any time during the iterations then C is outside the mandelbrot set. When C is confirmed to be outside or inside we plot a corresponding white or black pixel at coordinate C. After N iterations we give up to make sure the program finishes.


For Ci = -2 To 2 Step 0.01
    For Cr = -2 To 2 Step 0.008
        Zr = Cr
        Zi = Ci
        For i = 1 To 100
            Zrt = (Zr * Zr - Zi * Zi) + Cr
            Zi = (Zr * Zi + Zi * Zr) + Ci
            Zr = Zrt
            If (Zr * Zr + Zi * Zi) > 4 Then Exit For
        Next i
       
        If i = 101 Then
            complexPlane.PSet (Cr, Ci), RGB(0, 0, 0)        'Looks like we are inside
        Else
            complexPlane.PSet (Cr, Ci), RGB(255, 255, 255)  'Infinity
        End If
   
    Next Cr
Next Ci