Mandelbrot Set

See all articles from CATS v8 n9
Author(s): Neal M. Hicks
Publication: CATS Newsletter
Pub Details: CATS v8 n9 (Volume 8, Issue 9)
Date: February 1991
Pages: 5-6

[This is a version of the Mandelbrot Set program sent in by Stuart Walton. It was converted to the 2068 by Stuart’s nephew, Neal. Neal converted the program from memory and had never used the 2068 before. Not a bad feat. – Ed]

MANDELBROT FRACTAL IMAGE printout, LowX=-2 HighX=2
MANDELBROT FRACTAL IMAGE printout, LowX=-2 HighX=2
   1 BORDER 1: PAPER 7: INK 0: CLS
2 PRINT "LowX and HighX define the left and right boundaries of the screen. HighY defines the top boundary of the screen, and the program calculates LowY so that the final image will be in prop-ortion when COPY-ed to an Alpha-com 32 printer. The Mandelbrot fractal lives inside a circle of radius 2, so entries that are not between -2 and +2 will yield solid black screens. I recommend your firstscreen be LowX=-2, HighX=2, and HighY=2, Striping to 11. Use this image to find areas you want to study closer. Run times24-144 hrs. See REMs for more!"
3 REM program NEAL
5 INPUT "LowX";lx: INPUT "HighX";hx: LET dx=hx-lx
   6 INPUT "HighY";hy: LET ly=hy-dx*22/26.5: LET ix=dx/256: INPUT "Striping(1 to 97)";stripe: CLS
7 LET dy=hy-ly: LET iy=dy/176
20 LET e=hy: FOR y=175 TO 0 STEP -1: LET d=lx: FOR x=0 TO 255: LET f=d: LET g=e: LET n=1
21 IF f*f+g*g>=4 THEN GO TO 30
22 LET n=n+1: LET ff=f*f-g*g+d: LET g=2*f*g+e: LET f=ff: IF n>=100 THEN GO TO 40
23 GO TO 21
30 IF n>stripe THEN GO TO 41
31 IF n/2=INT (n/2) THEN GO TO 41
40 PLOT x,y
41 LET d=d+ix: NEXT x: LET e=e-iy: NEXT y
50 INPUT "Enter any key to print";a$: COPY
51 LPRINT "MANDELBROT FRACTAL IMAGE": LPRINT "LowX=";lx: LPRINT "HighX=";hx: LPRINT "LowY=";ly: LPRINT "HighY";hy: LPRINT "Black stripes 1-";stripe;" odds": LPRINT "Central black area is n=100.": INPUT "Print another COPY (y/n)";a$: IF a$="y" THEN GO TO 50
52 GO TO 1
1000 REM **MANDELBROT REVEALED** *****By Neal Hicks*****
1010 REM Fractal Math is a new and exciting field, which is allowing scientists to under- stand natural processes which had up till now been eluding normal math techniques. Chaos is the language of nature, from branches of a tree to turbulancein the air to the twisting net- work of capillaries in the humanbody. Fractal math describes all these simply and elegantly, while 'normal' math would fail utterly.
1020 REM The Fractal equation this program explores was disc- overed by Benoit B. Mandelbrot while he was working for IBM. Any equation in the form I will describe below will produce a fractal shape, but this was one of the first and is considered a 'classic' as a result.
1030 REM A fractal image lives on the complex plane, not on a cartesian coordinate grid. On acartesian grid, location is measured by 2 numbers (x,y), so many locations share the same x or y address.
1040 REM On the complex plane, the x-axis is the axis of Real Numbers, and the y-axis is the axis of Imaginary Numbers, in units of the square root of -1. Together, these two coordinates define a unique complex address in the form (x+yi) where 'i' is to indicate the imaginary part. Each spot on the complex plane is a unique complex number.
1050 REM The equation for the Mandelbrot fractal is amazingly simple:
1060 REM NewZ=(OldZ)squared+C.
1070 REM It is a recursive form-ula, applied over and over to each pixel on the screen until certain conditions are met. Thevariables lx,nx,ly and hy define
the location of each edge of thescreen's 'field of view' on the complex plane, and calculations using increments ix and iy with-in loops x and y assign a uniqueaddress on the complex plane to each pixel on the screen, in theform (d+ei), which is the complex constant 'C' in the equation  above.  Also, for each pixel,   'Z' is (f+gi), which is initially set to 0 (0+0i).  'n' counts  how many recursion  have been   performed.
1075 REM (NOTE--Since 'Z' is 0 and the first recursion yields (0)squared+C=C, I skip a step online 20 by just setting f and g equal to d and e, and setting n=1 rather than 0, and going directly to the first test at line 21)
1080 REM You are wondering, how can a computer do math involvingthe square root of -1? All you have to do is keep the real and imaginary parts separate. Thus:
1090 REM New(f+gi)=Old(f+gi)↑2+ (d+ei)
1091 REM =Old(f↑2+2fgi+(gi)↑2)+ (d+ei)
1092 REM =Old(f↑2+2fgi-g↑2)+(d+ei)*****rememembering that i*i=-1
1093 REM Now, separate the realsfrom the imaginaries:
1094 REM New(f)=Old(f↑2-g↑2+d)
1095 REM New(g)i=Old(2*f*g+e)i
1100 REM This is the guts of theprogram, the recursion on Line 22. ff is a temporary holder for Newf, since we need to pres-erve Oldf for the 2nd part of the formula. f=ff sets things straight, and the counter n is upped. Once the recursion of the pixel's complex address is performed, we jump to the test at 21 unless we've recursed 100 times without satisfying 21's test. If so, we are in the central mass of the fractal set, andwe jump right to 40, turn the pixel black.
1110 REM Now I'll explain what 21's test is for, what 100 recursions has to do with anything and what the stripes are for.
1120 REM The Mandelbrot image isthe mathematical limit of thel recursive equation I described above. That is to say, the picture this program produces isa picture of how the equation behaves when recursed. You start out with a pixel's complexaddress in the screen's field ofview, and each recursion of thatinitial complex number produces another one, and another, so theresulting numbers jump about on the complex plane, since each complex number is a unique spot on the plane. And, like a turtle race, the 'goal line' is a circle of radius 2 from the origin (0+0i) which line 21 tests for.
1130 REM If the point in question exits the circle in less than 100 recursions, we call that spot divergent, and color it according to n in alternating
areas of black and white to     distinguish areas of similar    behavior (lines 30-40).  The    area outside the circle of radius 2 is n=1, black.  The next    stripe in is n=3, then n=5, and so on up to the value of Stripe.
1140 REM If n=100 and we are still in the circle, we call thepoint convergent, or divergent so slow that we got bored and gave up. This is the central mass of Mandelbrot, whose edge bristles with extravagent detailwaiting to be explored with thisprogram.
1150 REM The reason to stop with100 recursions is that each areaof similar behavior (value of n)fits closer and thinner, like skins on an onion. There isn't much difference between n=99 andn=100, or for that matter n=200.Setting the limit to 200 would double run time without any realbenefit, and the program runs slow enough! For the same reason, if you aren't zooming in close and you stripe to 97, the edgeof the fractal will be obscured in a haze of stripes too close to be distinguished. In the starting image I suggest at the start of the program, use stripeto 11. If you zoom in to small areas, and the unstriped area islarge, add more cautiously.
1160 REM Each area where the stripes are disturbed or stretched out represents a branch, perhaps too small to see, of thecentral fractal pushing them outlike a wake before a ship. You may even find a tiny version of the larger image hanging by an unseen strand, but it will stillbe as different from its parent as a snowflake, full of detail of abstract, otherworldly beauty.
1170 REM Enjoy!--Neal M. Hicks [address omitted] Commodore-64 user.

Note: Type-in program listings on this website use ZMAKEBAS notation for graphics characters.