Friday, January 18, 2019

Mandelbrot BASIC Program



Mandelbrot BASIC Program






Here's another cool bit of code to cut and paste into BASIC emulator.

Same as previous post: download and install a BASIC emulator onto your computer, and paste the code in. I used "PC-BASIC" made by Rob Hagemans because it is one of the few emulators that also emulates graphics! I got it off a site called SourceForge. A lot of BASIC/QBASIC/GW-BASIC emulators don't do graphics. PC-BASIC does. Worked for me on a modern Lenovo ThinkCentre desktop   https://sourceforge.net/projects/pcbasic/

PC BASIC also one of the few emulators that seems to allow pasting lines of code in. That sure beats retyping everything.  I typed this program into an email at some point. Then I just copied it in the email program like normal (Control + C) and pasted into into PC-BASIC by hitting F11+V and it all appeared. Then type RUN and Mandelbrot stuff appears.


There are an infinite number of fractions between any two whole numbers: between 1 and 2 there are 1/2, 1/4, 1/8, 1/1000, 1/2304049904, 1/9994848483292929, etc. The dark set are the Mandelbrot numbers. The colors are numbers outside the set, blasting toward infinity.

Zoom in for a closer look, and there is always a ton of similar detail to see: fractals!

Here's the code to cut and paste (remeber to use F11 + V to paste once you're inside PC BASIC):


10           DEFSNG A-Z
20           SCREEN 1: KEY OFF
30           MAXDWELL = 150
40           NUMCOLORS = 4
50           NUMROWS = 100
60           NUMCOLS = 100
70           YOFFSET = 1
80           XOFFSET = 1
90           INPUT "LOWER LEFTHAND CORNER, REAL PART"; ACORNER
100         INPUT "LOWER LEFTHAND CORNER, IMAG. PART"; BCORNER
110         INPUT "LENGTH OF SIDE"; SIDE
120         CLS
130         COLOR 1
140         LINE (0, 0)-(NUMCOLS + XOFFSET, 0)
150         LINE (NUMCOLS + XOFFSET, 0)-(NUMCOLS + XOFFSET, NUMROWS + YOFFSET)
160         LINE (NUMCOLS + XOFFSET, NUMROWS + YOFFSET)-(0, NUMROWS + YOFFSET)
170         LINE (0, NUMROWS + YOFFSET)-(0, 0)
180        LOCATE 17, 1
190         PRINT "PERCENTAGE COMPLETE = 0"
200         PRINT "DWELL FOR LAST PIXEL = 0"
210         PRINT "LARGEST DWELL = 0"
215         PRINT "MAXDWELL ="; MAXDWELL
220         PRINT "REAL PART = "; ACORNER
230         PRINT "IMAGINARY PART = "; BCORNER
240         PRINT "SIDE =  "; SIDE
250         HIGHDWELL = 0
260         GAP = SIDE / NUMROWS
270         AC = ACORNER
280         FOR X = XOFFSET TO NUMROWS - 1 + XOFFSET
290         AC = AC + GAP
300         BC = BCORNER
310         FOR Y = YOFFSET TO NUMCOLS - 1 + XOFFSET
320         BC = BC + GAP
330         AZ = 0
340         BZ = 0
350         COUNT% = 0
360         SIZE = 0
370         WHILE (SIZE < 4) AND (COUNT% < MAXDWELL)  
380         TEMP = AZ * AZ - BZ * BZ + AC
390         BZ = 2 * AZ * BZ + BC
400         AZ = TEMP
410         SIZE = AZ * AZ + BZ * BZ
420         COUNT% = COUNT% + 1
430         WEND
440         COLOR 1
450         LOCATE 18, 23  
460         PRINT COUNT%; " ";
470         IF (COUNT% < MAXDWELL) AND (COUNT% > HIGHDWELL) THEN HIGHDWELL = COUNT%: LOCATE 19, 16: PRINT HIGHDWELL
480         IF COUNT% = MAXDWELL THEN PSET (X, NUMROWS - Y + 1), 0 ELSE PSET (X, NUMROWS - Y + 1), COUNT% MOD (NUMCOLORS - 1) + 1
490        NEXT Y
500         LOCATE 17, 22
510         PRINT 100 * X /NUMCOLS; " ";   
520         NEXT X
530         AS = INPUT$(1)            

                

                       
Once you run it you'll be asked to input three numbers. Here's the starter version:

-2
-2
4

these three inputs will give you the same Mandbrot as above.



Other numbers suggested are:
-.114
.917
.017

You end up zooming into the Mandelbrot, but since it's like fractals it looks very similar (but never the same). Sort of like the chaos in the last post's bit of code.

You can zoom in too much and get crazy numbers like 1.2453245E-9 in the readouts. If you've got the time and computing power you can try this:

10 DEFDBL A-Z




This Mandelbrot code really grinds the hard drive as it works. I used the default 20 Line of Screen 1. You can up the ante for more colors and better resolution by changing the lines, same as last post. Each variation increases colors and/or resolution/and or size but always makes it take longer to actually render onscreen:

20 SCREEN 7 : KEY OFF
40 NUMCOLORS = 16


Lots more colors:

20 SCREEN 13 :KEY OFF
40 NUMCOLORS = 256


Here's one that gives you 16 colors but increases the image size from 100 rows and columns to 200:

20 SCREEN 9 :KEY OFF
40 NUMCOLORS = 16
50 NUMROWS = 200
60 NUMCOLS = 200


Like in the last post, if everything goes haywire, it's probably because your graphics card can't handle this program...even though it's like two decades later chaos, infinities, bifurcations and fractals use a lot of computing power. Plus, you're going through a BASIC emulator which is going to slow things down too.

How slow? Here's a video I took with Line 20 Screen set to only "1" and the colors limited to only 4. The little window is only 100 rows x 100 columns. Look how slow it goes!