10 DATA ================== MANDSET ==================== 20 REM Version 1.0 30 DATA 40 DATA by Olivier A. Moreau B.Sc. 1985 50 DATA 60 REM Written in BASICA on an IBM PC with a color monitor 70 DATA ----------------------------------------------- 80 REM The goal of this program is to 90 DATA Explore the Mandelbrot Set in the Complex Plane 100 DATA ----------------------------------------------- 110 DATA | ==== MENU === | 120 DATA | | 130 DATA | <1> Go for One Trip | 140 REM INPUT the parameters following ten prompts 150 DATA | | 160 DATA | <2> Go for Many Trips | 170 REM INPUT the parameters in the DATA statements at end of program 180 DATA | | 190 DATA | <3> View Pictures | 200 REM OUTPUT the PICture files 210 DATA | | 220 DATA | <4> Exit | 230 DATA | | 240 DATA ----------------------------------------------- 250 : 260 REM--------initialize----------- 270 KEY OFF : OPTION BASE 1 : DIM PP%(160,160) 280 NP$="TEST1" : DP$="y" 290 RO=-2 : IO=-1.25 : SC=2.5 : SW%=20 : NI%=19 : CM=0 : CP=1 : CS=3 : DP=1 300 RN$="DEMO" : FP=1 : LP=3 310 DEF FNB$(IN)=STRING$(2+LEN(STR$(IN))-(IN<0),29) : REM Backspace cursor 320 REM============================ Title & Menu ===================== 330 REM>>> LOOP MENU <<< 340 SCREEN 0 : WIDTH 80 : COLOR 7,0 : CLS 350 RESTORE 10 : FOR I%=1 TO 18 : READ DA$: PRINT " ";DA$ : NEXT I% 360 IN$=INKEY$ : IF IN$="" THEN 360 370 IF NOT (IN$="1" OR IN$="2" OR IN$="3" OR IN$="4") THEN BEEP : GOTO 360 380 MO=VAL(IN$) 390 ON MO GOTO 440,650,810,400 400 CLS : PRINT "Goodbye..." : PRINT : END 410 : 420 REM============================ INPUT ============================ 430 REM--------------- One Trip ---------- 440 CLS : PRINT "Keep or change these parameters:" : PRINT : PRINT 450 PRINT : PRINT "Name of the picture ";NP$;STRING$(2+LEN(NP$),29); : INPUT IN$ : IF IN$<>"" THEN NP$ = IN$ 460 NP$=NP$+".PIC" 470 PRINT : PRINT "Real origin ";RO;FNB$(RO); : INPUT IN$ : IF IN$<>"" THEN RO=VAL(IN$) 480 PRINT : PRINT "Imaginary origin ";IO;FNB$(IO); : INPUT IN$ : IF IN$<>"" THEN IO=VAL(IN$) 490 PRINT : PRINT "Size of the complex plane explored ";SC;FNB$(SC); : INPUT IN$ : IF IN$<>"" THEN SC=VAL(IN$) 500 PRINT : PRINT "Size of the screen window ";SW%;FNB$(SW%); : INPUT IN$ : IF IN$<>"" THEN SW%=VAL(IN$) 510 IF SW% > 160 THEN SW%=160 : BEEP : PRINT "Too large..." : GOTO 500 520 PRINT : PRINT "Number of iterations ";NI%+1;FNB$(NI%); : INPUT IN$ : IF IN$<>"" THEN NI%=VAL(IN$)-1 530 IF NI% > 1000 THEN NI%=19 : BEEP : PRINT "Too many..." : GOTO 520 540 PRINT : PRINT "Color of the set ";CM;FNB$(CZ); : INPUT IN$ : IF IN$<>"" THEN CM=VAL(IN$) 550 IF CM<0 OR CM>16 THEN CM=0 : BEEP : GOTO 540 560 PRINT : PRINT "Palette (0 or 1) ";CP;FNB$(CP); : INPUT IN$ : IF IN$<>"" THEN CP=VAL(IN$) 570 IF CP<0 OR CP>1 THEN CP=1 : BEEP : GOTO 560 580 PRINT : PRINT "Coloring scheme (1 to 3) ";CS;FNB$(CS); : INPUT IN$ : IF IN$<>"" THEN CS=VAL(IN$) 590 IF CS<1 OR CS>3 THEN CS=3 : BEEP : GOTO 580 600 PRINT : PRINT "Show calculation, ruler and description of picture (y/n) ";DP$;STRING$(3,29); : INPUT IN$ : IF IN$<>"" THEN DP$=IN$ 610 IF DP$="y" OR DP$="Y" THEN DP=1 : GOTO 890 620 IF DP$="n" OR DP$="N" THEN DP=0 : GOTO 890 630 DP$="y" : BEEP : GOTO 600 640 REM-------------- Many Trips --------- 650 RESTORE 2120 : READ RN$,FP,LP 660 CLS : PRINT "Calculating pictures: ";RN$;FP;" to ";RN$;LP 670 PRINT : PRINT "Is this what you want (y/n) y";STRING$(3,29); : INPUT IN$ 680 IF IN$="Y" OR IN$="y" OR IN$="" GOTO 730 690 IF NOT (IN$="N" OR IN$="n") THEN BEEP : GOTO 670 700 PRINT : PRINT "You should have changed this DATA statements:" : PRINT 710 LIST 2090- : REM For compiled version, replace by "IF INKEY$="" GOTO 690" and give more explanation in 680 720 END 730 PC=FP-1 740 REM>>> LOOP MANY <<< 750 PC=PC+1 760 NP$=RN$+MID$(STR$(PC),2)+".PIC" 770 READ RO,IO,SC,SW%,NI%,CM,CP,CS,DP 780 NI%=NI%-1 790 GOTO 890 800 REM--------------- View Pictures -------- 810 CLS : PRINT "The following PICtures files are available:" : PRINT 820 FILES "*.PIC" 830 PRINT : PRINT "Root name of the picture ";RN$;STRING$(2+LEN(RN$),29); : INPUT IN$ : IF IN$<>"" THEN RN$ = IN$ 840 PRINT : PRINT "First picture number ";FP;FNB$(FP); : INPUT IN$ : IF IN$<>"" THEN FP=VAL(IN$) 850 PRINT : PRINT "Last picture number ";LP;FNB$(LP); : INPUT IN$ : IF IN$<>"" THEN LP=VAL(IN$) 860 PRINT : PRINT "Press ANY KEY to see the pictures..." 870 IF INKEY$="" GOTO 870 ELSE GOTO 1430 880 REM--------------- Adjust Parameters, Initialize more variables -------- 890 SP=SC/SW% : MI%=NI% : MA%=0 : AV=0 : DZ#=0 : PO%=0 900 : 910 REM============================ PROCESSING ======================= 920 CLS : WIDTH 80 : PRINT "Calculating ";NP$;" for a while..." 930 IF DP THEN IF MO=1 THEN PRINT : PRINT"Row Col Z Iterations" 940 FOR R%=1 TO SW% : FOR I%=1 TO SW% 950 REM-------------- Search Complex Number Z ------------- 960 RC=R%*SP+RO : IC=I%*SP+IO : RZ=0 : IZ=0 : ZZ%=0 970 TZ=RZ : RZ=RZ*RZ-IZ*IZ+RC : IZ=2*TZ*IZ+IC : ZZ%=ZZ%+1 : DZ=SQR(RZ*RZ+IZ*IZ) 980 IF (DZ>2) OR (ZZ%>NI%) GOTO 1000 ELSE GOTO 970 990 REM-------------- Store zz%, Find min, max, average, "distances" ----- 1000 PP%(R%,I%)=ZZ% 1010 IF ZZ%MA% THEN MA%=ZZ% 1030 IF MO<>1 GOTO 1080 1040 IF DP<>1 GOTO 1080 1050 IF ZZ%>NI% THEN COLOR 23,0 : ZZ%=20 : GOTO 1070 1060 IF ZZ%<14 THEN COLOR ZZ%,0 ELSE COLOR 15,0 1070 PRINT R%;I%;DZ ; : LOCATE CSRLIN,30 : PRINT STRING$(ZZ%,219) 1080 NEXT I%,R% 1090 IF PO%>0 THEN AV=AV/PO% 1100 IF CS=1 THEN SN=MI% : MN=AV : LN=MA% 1110 IF CS=2 THEN SN=MI%+(AV-MI%)*2/3 : MN=AV+(MA%-AV)/3 : LN=MA% 1120 IF CS=3 THEN SN=AV : MN=AV+(MA%-AV)/2 : LN=MA% 1130 : 1140 REM============================ OUTPUT =========================== 1150 REM---------- Statistics ------------ 1160 IF NOT (DP=1 AND MO=1) GOTO 1240 1170 COLOR 7,0 1180 PRINT : PRINT "Number of pixels in set";SW%^2-PO% 1190 PRINT "Number of Iterations: Minimum, Average, Maximum";MI%;AV;MA% 1200 PRINT "Number of Iterations: Small, Medium, Large";SN;MN;LN 1210 PRINT : PRINT "Press ANY KEY to see the picture" 1220 IF INKEY$="" GOTO 1220 1230 REM---------- Description ----------- 1240 SCREEN 1,0 : CLS : COLOR CM,CP 1250 IF DP<>1 GOTO 1300 1260 LOCATE 25,10 : PRINT "RO=";RO ; 1270 LOCATE 24,1 : PRINT "IO=";IO 1280 LOCATE 1,1 : PRINT LEFT$(NP$,LEN(NP$)-4);" SC=";SC;"NI=";NI%+1 1290 REM---------- Ruler ----------------- 1300 XO%=(320-SW%)/2 : YO%=((200-SW%)/2)+SW% 1310 IF DP THEN D%=SW%/20 : XR%=XO%-D% : YR%=YO%+D% : FOR R%=D% TO SW%+D% STEP 2*D% : PSET (XR%+R%,YR%),1 : PSET (XR%,YR%-R%),1 : NEXT R% 1320 REM---------- Picture --------------- 1330 FOR R%=1 TO SW% : FOR I%=1 TO SW% 1340 X%=XO%+R% : Y%=YO%-I% 1350 IF PP%(R%,I%)<=SN THEN CO=1 : GOTO 1390 1360 IF PP%(R%,I%)<=MN THEN CO=2 : GOTO 1390 1370 IF PP%(R%,I%)<=LN THEN CO=3 : GOTO 1390 1380 CO=CM 1390 PSET (X%,Y%),CO 1400 NEXT I%,R% 1410 GOTO 1520 1420 REM----------Saved pictures---------- 1430 SCREEN 1,0 1440 FOR I%=FP TO LP 1450 SP$=RN$+MID$(STR$(I%),2)+".PIC" 1460 DEF SEG= &HB800 : BLOAD SP$,0 1470 IF INKEY$="" GOTO 1470 1480 NEXT I% 1490 GOTO 1550 1500 : 1510 REM=========================== Saving Picture on Disk =========== 1520 DEF SEG = &HB800 : BSAVE NP$,0,&H4000 1530 : 1540 REM=========================== Looping ========================== 1550 IF MO=2 AND PC>> 1560 IF MO<>1 GOTO 340 : REM <<< LOOP MENU >>> 1570 IF INKEY$="" GOTO 1570 ELSE GOTO 340 : REM <<< LOOP MENU >>> 1580 : 1590 REM=========================== Documentation ==================== 1600 REM ALL variables' name are only two letters long to facilitate conversion to other BASICs. 1610 REM Variables are described by order of appearance in the program. 1620 : 1630 REM PP% matrix of the Pixels of the complex Plan (160*160 is max for IBM PC's memory) 1640 REM NP$ Name of Picture (made of: root=MANDSET, number=1, extension=PIC) 1650 REM DP$ Describe Picture string ("y" or "n") 1660 REM RO Real Origin 1670 REM IO Imaginary Origin 1680 REM SC Size of the Complex plan 1690 REM SW% Size of the Window 1700 REM NI% Number of Iterations 1710 REM CM Color of the pixels in the Mandelbrot set 1720 REM CP Color of the Palette (3 variables are needed for other computers) 1730 REM CS Color Scheme (emphasis on: 1 small NI, 2 medium NI, 3 large NI) 1740 REM DP Describe Picture (yes=1, no=0) 1750 REM RN$ Root Name of the picture (6 chars at max) 1760 REM FP First Picture number 1770 REM LP Last Picture number 1780 REM I% loop variable 1790 REM DA$ temporary storage of DAta for title screen 1800 REM IN$ temporary INput string 1810 REM MO Menu Option 1820 REM PC Picture number Counter (also used to append to RN$) 1830 REM SP Size of one Pixel 1840 REM MI% MInimum number of iterations 1850 REM MA% MAximum " " 1860 REM AV AVerage " " (also total used to find average) 1870 REM DZ Distance of the complex number Z 1880 REM PO% counter of Pixels Outside of mandelbrot set 1890 REM R% loop variable 1900 REM RC Real part of the complex number C 1910 REM IC Imaginary " " 1920 REM RZ Real part of the complex number Z 1930 REM IZ Imaginary " " 1940 REM ZZ% counter of iteration (ZZ is for ZZZ... (you can have a nap)) 1950 REM TZ Temporary value equal to RZ to calculate IZ (RZ and IZ must be calculated in parallel) 1960 REM SN Small Number of iterations 1970 REM MN Medium " " 1980 REM LN Large " " 1990 REM XO% X axis Origin 2000 REM YO% Y " " 2010 REM D% Distance between picture and ruler 2020 REM XR% X coordinate of Ruler 2030 REM YR% Y " " 2040 REM X% X coordinate of a pixel 2050 REM Y% Y " " 2060 REM CO COlor of the pixel 2070 REM SP$ Saved Picture name 2080 : 2090 REM=========================== DATA ============================= 2100 REM RN$,FP,LP 2110 : 2120 DATA "DEMO",1,3 2130 : 2140 REM RO,IO,SC,SW%,NI%,CM,CP,CS,DP 2150 : 2160 DATA -2,-1.25,2.5,160,20,0,1,3,1 2170 DATA -1.5,-.15,.3,160,20,0,1,3,1 2180 DATA -1.25,.15,.2,160,20,0,1,3,1