Difference between revisions of "Mandelbrot set"
Line 50: | Line 50: | ||
− | [[Category: | + | [[Category:General information]] |
Revision as of 14:47, 23 March 2008
The formula for the mandelbrot set using complex numbers is:
- Z = Z2 + C
If Z escapes to infinity then we are outside 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 and assume that Z will never escape to infinity to make sure the program finishes in a reasonable time.
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 'Give up after 100 iterations to trade accuracy for speed 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