GWBasic Graphics


Mandelbrot Series


With all the programming techniques we have developed so far, and with the use of the GWBasic Users Manual that we can download from the Useful Links section of our wee little tutorial, we have the tools to begin creating some of our own programs that do more than demonstrate the ability of GWBasic to permit us to program under DOS and  the Windows Operating System.

I realize that since you are this far along with GWBasic, that is no longer necessary for me to provide encouragement because you are already motivated. By this time, it should also be known that we have hardly tapped the power available to us with software that is already available to us, if we take the time to learn how to use it.

Enough of these mutual admiration society words. Lets get into the Mandelbrot series. Computer graphics is a fascinating field of interest. Fractals and other special graphic representation of mathematical functions speak for themselves if we have the tools to reproduce them. Lets look at the famous Mandelbrot Series.

Due to the length of this example, I will make this format a little different. The program itself will have its own link. Also a "softalk" write-up will be in another link from this page to keep this page a reasonable length.



What is this example all about

At first glance the plot usually displays a square field with a dark image in the center that has an indentation on one end and is pointy on the other. There are wee little squiggles that come out of the main central image. More elaborate displays have colors and there is an inclination to zoom in on parts of the image to see what is there.

Zooming in on any part of the image seems to create more fascinating images. It also seems that no matter how far we zoom in, that the enlarged image becomes even more fascinating. There are a large number of programs for sale, and free for the downloading from the web that we can google and run to reproduce the Mandelbrot series. There are also a large number of images already created for viewing and copying too. Makes me want to say " please Mother, I'd rather do it myself" so here is a graphics program that will do just that..

This example is an attempt to share some of the algorithms I developed to do just that. It is also an open invitation for you to jump in with both feet and make a program that will do it the way you decide to control it. Here goes......  






How to desplay the series in a two dimensional plot


The commonly recognized plot is a square field using an x,y plot. The WINDOW is from (-2,-2) to (2,2). The VIEWPORT begins with the same field of co-ordinates and can be reduced to zoom in on a selected section of the WINDOW to see more and more detail. This plot will use the best Graphics Screen Mode available to me under GWBasic.

Each point within this plot is examined with the expression that defines the series for its properties and a dot on the plot represents the properties of this point. I begin with the lowest (x,y) point and test it. I chose to step through all the points in the WINDOW with two nested FOR/NEXT loops:

FOR X= -2 to 2 
FOR Y= -2 to 2
     Examine this point to determine it's properties
     PSET a dot to represent this point's properties
NEXT Y
NEXT X


Not very much to talk about? Read on and see for yourself. This is a program to easily get very involved with.
For example, evaluating each point in the field of interest can be enlightening. The Mandelbrot series is not all that difficult to display, although it takes a lot of number crunching. Lets look at the evaluation part of the nested loops we just described:

Each of the points within a square is examined for its properties with a math series expression, an itterative loop that requires a previous calculation to have the numbers for a new calculation.

The simplest example might be a counter that increments with each step. The expression would be  new N= old N + 1. Our intent might be to determine how many times through the loop it would take for the new N to reach the number 100. Again, we know what the answer would be, but this is just a simplified example. Here is how I would do it. I would insert this code in the line above that says "examine this point to determine it's properties":

         let INDEX= 0              initalize my counter.
         let            n= 0       initalize my value of N.
         let            c= 1       set my increnent value to counting by ones.
REPEAT   n = n + c                 calculate my new N.
         index= index + 1          increment my count, once per iteration.
      If index < 100 then REPEAT   quit when the terminating criteria has been met, else repeat
         print INDEX               display the results. and quit.




We already know that it takes 100 times throu the loop so the display will be a print out of 100. But, the same scheme can be used to evaluate the Mandelbrot Series using a little more sophisticated arithmetic. Non the less, the same scheme is used where:
     new Z= Z2+ C   Where Z begins at zero and C is a point on the plot.
             If C   becomes greater than 2 or the Index count reaches some selected high figure then quit.
     then, the value in Index determines what color the point on the display will be.

Since this plot would be a single dimension, then the plot would be a line consisting of a series of dots beginning with Z=0 and increasing until the final count is reached. The color of the do would represent the properties of the point. If we whished to extend this into a second dimension to make an X,Y plot, then all we need to do is use complex numbers. This is actually what we are going to do:



Here is where this series gets fun.

Z and C are both complex numbers and can therefore be plotted on the absissa and ordinage of a two dimensional graph, using colored pixel dots to represent the properties of that point of interest. Our completed plot will represent all the points within our 2 by 2 field of interest. Our program does, in less than a minute, what it would take the months for the dinos to do by hand with pencil and paper.

This is just a review of complex numbers so you may wish to skip it, but the words should be said in this introduction for completeness.

Z and C are complex numbers, with a real and complex component. The for addition and other math processes are done with a the same as regular math, but an additional step is involved. The squared factor, takes us into a second dimension, so we get into geometrical figures. Its no problem; though, because it still follows a simple mechanical process. We need an operator to drive our line into another right angle plane. Fortunately, its already been invented and it is usuaually represented with the letter i or j. What it does for our plot is rotate everything 900 so we can represent it in a two dimensional plot. So this gives our Z and C a new dimension and represented with two values, a real and a complex value.
Z looks like (Z + Zi) and the constant
C looks like (C + Ci).
The real and the complex elements can easily be plotted on a two dimensional graph. Its easy enough to do if  i  is given the value of the square root of a minus 1. If we square the complex value of  ,then it becomes a real number with the value of plus one.
By definition, the square root of a number, squared, is indeed that number. Therefore, i times i = 1.  

Lets look at the first couple of steps in our plot:
Z begins at zero or (0 + 0i) and C begins at (-2 + -2i) at a corner of our virtual field on our plot.
Lets calculate our new Z:

Z squared= (0+0i) times (0+0i) and equals ( 0+0i)
   Add the real and complex values of C   (-2+2i)
                   and the new Z becomes  (-2+2i)

we increment our counter, and do it again because the answer is outside our test limits.
Substitute into the general formula  (a + b)2 =  (a+b)(a+b) or a2+ab+ab+b2  keeping in mind that our b term is an i factor.

New Z is Z2 = (-2 + 2i)x (-2 + 2i) or
                -2  x -2 = (4 + 0
i  )    = ( 4 + 0i)
         plus   -2  x 2i = ( 
- 4i  )    = ( 0 + 4i)
         plus   -2  x 2i
= (  - 4i  )    = ( 0 + 4i)
         plus    2i x 2i = (  + 4i2)  = ( 4 + 0i)  because i squared = a plus 1
Add the terms and our new Z2 becomes    ( 8 + 8i)
Add the constant C                      (-2 - 2i)
The final new Z becomes                 ( 6 + 6i)

Since either 6 or 6i is greater than two, this point is not part of the set.
So... we go on to the next point with a count of only 1 in our accumulator and color it appropriately on the chart.

Then we go on to our next Value of (C+Ci) with a brand new value for Z = (0+0i)
We do this same thing for all our points on the chart.







Summary
Link to the program and copy-paste it into a text file so you can run it with the GWBasic Interpreter.
Play around with the program until you run across something that you don't understand. Then, like all good programmers, when all else fails, read the instructions. A very complete write-up on the program can be called up with the Softalk link. This will be a program that you will undoubtedly use as a skeleton for many other graphic programs that you want to develop on your own.


In short.... play around and have fun.........  Happy Hacking......