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:

`DIM`

Create an array variable

`LET`

Assigns a value to a variable

`FOR`

… `NEXT`

Basic iteration loop structure

`PRINT`

Display information to the screen

`INT()`

Return the integer portion of a number

`RND()`

Return a random number between 0 and 1

And for the graphics, these statements:

`GR`

or `HGR`

Set standard graphics (GR) mode or high-resolution graphics (HGR) mode

`COLOR=`

or `HCOLOR=`

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

`PLOT`

or `HPLOT`

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:

- Set three points that define a triangle
- Randomly select a point anywhere (
*x*,*y*)

- Randomly select one of the triangle's points
- Set the new
*x*,*y*to be the midpoint between the previous*x*,*y*and the triangle point - Repeat

```
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

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

`GR`

to set standard graphics mode, and set the draw color to blue (7):`GR`

COLOR= 7

`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`

...

`NEXT I`

`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))`

`LET XN = INT ((XX + X(IX)) / 2)`

LET YN = INT ((YY + Y(IX)) / 2)

`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:

`LET XX = XN`

LET YY = YN

PLOT XX,YY

`I`

before starting the loop over again:`PRINT I`

(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.