ժҋ GW-BASIC ᛛดቱ߸ᛜވЈݛ ఀήച ౺ज़
Using GW-BASIC for Drawing Mandelbrot Sets
Mr. LEUNG Chi Kit
The Hong Kong Taoist Association Ching Chung Secondary School
For a given complex number c0, define the sequence of complex numbers { cn }by cn+1 = cn2 + c0 for n = 0 , 1 , 2 , …. If the sequence is bounded, that is, we can find a real number M such that for every n, | cn | < M, then c0 is said to belong to the Mandelbrot set.
We can use the definition above to write a GW-BASIC programΰSorryΜI always like to use the simplest computer language and I think it is not difficult to translate a GW-BASIC program to other computer languageαas followΚ
10 LEFT = 150 : TOP = 380 : W = 360 : M = .833 20 R = 2.64 : S = 2 * R / W 30 RECEN = 0 : IMCEN = 0 40 SCREEN 9 : CLS 50 FOR Y = 0 TO W 60 FOR X = 0 TO W
70 REC = S * (X – W / 2) + RECEN : IMC = S * (Y – W / 2) + IMCEN 80 RE = REC : IM = IMC
90 RE2 = RE * RE : IM2 = IM * IM : J = 0 100 WHILE RE2 + IM2 <= 256 AND J < 15 110 IM = 2 * RE * IM + IMC
120 RE = RE2 – IM2 + REC
130 RE2 = RE * RE : IM2 = IM * IM : J = J + 1
140 WEND
150 IF J < 3 THEN GOTO 220
170 180 190 200 210 220 230 240 250 260 270
IF J >= 6 AND J < 9 THEN COLOR 1 : REM BLUE IF J >= 9AND J < 12THEN COLOR 2 : REMGREEN IFJ >= 12AND J < 15 THENCOLOR 15 : REMWHITE IF J >= 15 THEN
COLOR
12 : REM REDPSET (X + LEFT, (TOP -Y)
*
M) NEXT XNEXT Y
COLOR 15 : REM WHITE
LWE (LEFT, (TOP
-
W / 2)*
M)-
(W + LEFT, (TOP- W / 2)*
h4)LINE (W/ 2 + LEFT, (TOP-w)
*
M) - (W/ 2 + LEFT, TOP*
M) ENDThe following explains the program :
W sets the size of the picture to be drawn on the computer screen. Initially W is set to 360 (see line 10). This means we plan to draw the Mandelbrot set in a 360 x 360 square in the computer screen (see lines 50 and 60).
LEFT is the leftmost position of the picture on the screen,
TOP
is the lowest position of the picture (see lines 210, 250 and 260). Caution: in GW-BASIC, the coordinates of the computer screen go fkom top to bottom unlike our usual convention of going ftom bottom to top. So we have to use “TOP - Y” to convert the usual coordinate system to the computer screen coordinate system.Since a pixel on the computer screen is not a square, so the horizontal and vertical sizes are not the same, hence we introduce M ( =
2
) to adjust the length-tewidth ratio (see lines 10,210,250 and 260).Note W is only the size on the screen and not the actual coordinates of the complex numbers in the Mandelbrot set. So W needs to be transformed. R is the actual value. (see line 20). That is, for the range of the picture, the real axis goes fiom -R to R and the imaginary axis also goes h m -R to R S computes the ratio of W and R and is used in later computations ( see lines 20 and 70).
RECEN and IMCEN are used to locate the position ofthe center. The center is initialized to (0, 0) (see line 30). E3y changing the value of
R,
RECEN or IMCEN, we can move or dilate the Mandelbrot set.Line 40 chooses the format of the picture and erase the old screen.
Lines 50 and 60 of the program set the ranges of X and Y Then line 70 computes the real and imaginary parts of the corresponding co.
Observe that if cIl =
+ ho i
I c, = an + h,I,
thenC“+l = c.2 + CCl
= (a. + h. i)’ +
= an2 - h.2 + 2a,hn i + a0 + hl, i - (a, - h,’ + a()) + ( k , , h , + h0)i.
+ ho i )
- 2
So the real part of c,+, is an2
-
ha2 + an and the imaginary part is 2a,h + ho.Converting these computations to codes yield lines 110 and 120. REC and M C are the real and imaginary parts of c~ respectively. RE and lh4 are the real and imaginary parts of c,, respectively. RE2 and IM2 are the squares of the real and imaginary parts of c,, respectively.
J is a counter for running the loops in lines 100 and 140. Line 100 also computes the square of the modulus of c,. If the square of the modulus is greater than 256 or the loop has been executed 15 times, then we terminate the loop. Consequently, the larger the value of J is, the closer the sequence will tend to ‘‘converge”. That is, after many computations, the modulus of c. still does not get big. Lines 150 and 200use colors to classify the rate of convergences. Red indicates the complex numbers with fastest converegence, then comes white, green, blue and yellow. The region with the fastest divergence is indicated in black. Line 210 chooses the c o l a for the point.
After drawing the Mandelbrot set, we draw the horizontal and vertical axes in white (see lines 240 and 260) for reference. This concludes the program
The running time for the program depends on the speed of the computer. For the present computers, the whole program can finish in about a minute.
Reference
Heinz-Otto Peitgen, Hartmut Jiirgens and Dietmar Saupe (1992) Fractals for the Chmorn Part Two:
Introduction to Fractals and Chaos.