Step 4. If you use a GOSUB statement instead of GOTO, the program will jump to the line number specified; a RETURN statement returns it to the line after the GOSUB. The code between the lines referred to in the GOSUB and the RETURN is called a subroutine. These make code easier to read and allow you to use it in different parts of your program.
Step 5. We’ll use data in a separate file to draw a pattern. Using Notepad, save a file called DATA.TXT in the same folder as your SmallBasic programs. Enter the following values:
Step 6. Alter the previous walkthrough's Step 11 program. Add OPEN “DATA.TXT” FOR INPUT AS #1 to the beginning. Change the two READ statements to INPUT statements. These usually prompt you for input data, but the following variant reads data from the file number specified in the OPEN statement: ‘INPUT #1, X, Y’.
Step 7. SmallBasic features many functions that work with numeric values. The program shown below uses SIN and COS, but for trigonometric functions the angle must be in radians rather than degrees; D2R is a conversion factor. Note the rounding errors in the results – sin(90) is really 0.
Step 8. Enter PSET SCREENX, SCREENY COLOR 1 inside a FOR/NEXT loop that increments SCREENX from 0 to 599. Put this inside a FOR/NEXT loop that increments SCREENY from 0 to 399. You’ll see a black block. Temporarily put ‘DELAY 10’ after the PSET to see that it draws one dot at a time. Save this program.
Step 9. Start a new program. INPUT the real and imaginary parts, call a squaring subroutine (starting at line 500), and PRINT the answer. The real part is real squared minus imaginary squared; the imaginary part is two times real times imaginary. Test with real -= 0.1, imaginary = 1; the result should be real = -0.99, imaginary = 0.2.
Step 10. Next, INPUT two complex numbers. Alter the subroutine to add the second complex number to the result of squaring the first. The real part of the sum of two complex numbers is the sum of the real parts. The imaginary part is the sum of the two imaginary parts. Verify the results with an online complex calculator.
Step 11. Modify the previous program to INPUT a complex number and PRINT a message as to whether it’s in the Mandelbrot Set. To get the modulus square the real and imaginary parts, add them together and square root the sum. You should find that real = -1, imaginary = 0 is in the set, and real = -2, imaginary = 1 is not.
Step 12. Use Step 11’s logic inside the nested loops of Step 8’s program to test points on the complex plane, drawing a dot with the PSET statement only for points in the Mandelbrot Set. Test points from real = -2 and complex = 1 to real = 1, complex = -1. Derive these from SCREENX and SCREENY (for example Real = SCREENX/200-2).
Step 13. The Mandelbrot Set is often shown in colour, with points outside the Set displayed as contours. Modify the previous program to plot points that aren’t in the Set, using a colour derived from the count of how many iterations it required to exceed a modulus of two. Colour = 16 – Count / 8 works well.
Step 14. The Mandelbrot Set is a fractal figure. The more you zoom, the more detail you see. To fully appreciate it, modify the software to show an area for which you specify the real and imaginary parts of the complex number and a zoom factor. The screenshot below shows what you’ll see if real = -1.15, imaginary = 0.4, and zoom = 5.5.