Thursday, September 7, 2017

Dissecting the Sierpinski Triangle program

Last week, I shared my return to old school programming. My first home computer was an Apple II clone called the Franklin ACE 1000, and it was on this machine that my brother and I taught ourselves how to write programs in AppleSoft BASIC. And last week, I had an itch to return to the Apple II and write a simple BASIC program to generate the Sierpinski Triangle:

Let me briefly show you how to code the Sierpinski Triangle in AppleSoft BASIC. Fortunately, this is a simple program that only uses a few functions and statements:
Create an array variable

Assigns a value to a variable

Basic iteration loop structure

Display information to the screen

Return the integer portion of a number

Return a random number between 0 and 1

And for the graphics, these statements:
Set standard graphics (GR) mode or high-resolution graphics (HGR) mode

Set the color for drawing graphics in GR or HGR mode, respectively

Make a single pixel dot in GR or HGR mode, respectively

So with those instructions, I was able to iterate a chaos generation of the Sierpinski Triangle. Again, if you aren't familiar with this method to generate the Sierpinski Triangle, the brief rules are:
  1. Set three points that define a triangle
  2. Randomly select a point anywhere (x,y)
  1. Randomly select one of the triangle's points
  2. Set the new x,y to be the midpoint between the previous x,y and the triangle point
  3. Repeat
Let's start with the first step, to define the triangle's end points. I used two parallel single-dimension arrays, and assigned values to them. This assigns the points at 0,39 (left,bottom) and 20,0 (middle,top) and 39,39 (right,bottom):
DIM X(3)
DIM Y(3)
LET X(1) = 0
LET X(2) = 20
LET X(3) = 39
LET Y(1) = 39
LET Y(2) = 0
LET Y(3) = 39
Note that in AppleSoft BASIC, arrays start at 1. And of course, all lines must have line numbers, but I won't show those here.

Then I defined a random point anywhere on the board. Since it's random, it doesn't really matter what I pick, so I hard-coded this at 10,10:
LET XX = 10
LET YY = 10
To draw each pixel in the Sierpinski Triangle, we need to be in graphics mode. So I use GR to set standard graphics mode, and set the draw color to blue (7):
I chose to make 2,000 iterations on the triangle. Maybe this was overkill for GR mode, but it worked well in HGR mode, so I just left it. You start an iteration loop with the FOR statement that specifies the start and end values, and close the loop with the NEXT statement. Since you can have nested loops, the NEXT statement requires the variable you are iterating:
FOR I = 1 TO 2000
Inside the loop, I pick a triangle end point at random, then do some simple math to find the midpoint between the current XX,YY and the end point. AppleSoft BASIC only allows variable names up to two characters long. So here, IX stands for "index."

Technically, RND(1) returns a floating point value from 0 to 0.999…, so multiplying by 3 gives 0 to 2.999…. Adding 1 to this results in a floating point value between 1 and 3.999…. To use this as the index of an array, I want an integer, so I use the INT() function to return just the integer part, resulting in a final integer value between 1 and 3, inclusive:
LET IX = 1 + INT (3 * RND (1))
With that, I can compute the midpoint:
LET XN = INT ((XX + X(IX)) / 2)
LET YN = INT ((YY + Y(IX)) / 2)
Again, since AppleSoft BASIC only allows variable names of up to two characters long, XN represents "X new" and YN represents "Y new."

Then I reassign those values back to XX and YY, then plot the pixel on the screen:
And as a way to track the progress of the overall iteration, I print the counter I before starting the loop over again:
And that's how to create a Sierpinski Triangle in AppleSoft BASIC!

(Oops, I used floating point variables throughout, when I should have been using integer variables. My bad. If I were to code this again, I would use variables like XX% instead of XX. That would also obviate the need to use the INT() function, since the value would be automatically cast to integer when saving the value to the integer variable.)

BASIC is a very straightforward language, with only a few statements and essential functions. AppleSoft BASIC was not a very difficult language to learn. Even as a young child, I quickly figured out how to create simple math quizzes. From there, I graduated to larger and more complex programs, including one that effectively emulated the thermonuclear war simulator from the 1983 movie, War Games!

But I'll stop here, and leave it to you to learn more about BASIC on your own. You can find AppleSoft BASIC programming guides in lots of places on the Internet. Landsnail's Apple II Programmer's Reference is a good one.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.