.d64 image (needed for the graphics wedge software)

Since I wrote my Down & Dirty Graphics Wedge, I’ve been playing with a lot more graphics routines on my Commodore. As I’ve mentioned in the past, I’ve always loved fractals and loved how they come together. I wrote a program on my dad’s IBM XT computer when I was a kid to draw the Mandelbrot set. I recreated something similar on the Commodore using a combination of BASIC and machine language. You can check out that post here.

There are so many interesting things to try on the computer having a graphics utility for it. I picked up a book on programming fractals. This is a modern book and uses C++ as the programming language and often relies on recursion to generate the images for the fractals. This makes sense because many of the fractals you see are repetitive calculations within the algorithm.

Unfortunately, C64 BASIC does not have recursion and if it did, it would be fairly memory hungry. The maze program I discussed previously on this blog normally uses recursion but I was able to create my own stack to manage the data used for the algorithm.

A quick discussion about Sierpinski’s Triangle. If you are not familiar with it, it looks like a triangle with a triangle inside of it, with more triangles within those triangles. This goes in into infinity. To draw it by hand follow these steps:

  1. Draw a triangle.
  2. Find the midpoints of the 3 lines that make the triangle.
  3. Connect the midpoints to create a new triangle.
  4. From the three newly created triangles around your triangle repeat steps 2 through 4 infinitely.
Drawing Sierpinski

Well, no reason to do this to infinity, but you get the idea. This is also how a computer might do it as well, keeping track of all the triangles as they are drawn to reference them when needed. The memory for this kind of algorithm gets eaten up pretty quickly on the old C64.

Luckily, there is another way to draw Sierpinski’s Triangle without having to keep tabs on any triangles. As a matter of fact, no triangle get drawn at all, only points are plotted on the screen. The way it works is like this:

  1. Store the 3 points that would make your first triangle.
  2. Pick a random location (or specify one) within the three points.
  3. Pick a random number between 1 and 3. These represent one of the three points of the first triangle.
  4. Calculate the midpoint from your current location to the selected point of the first triangle and plot that point making it your new current location.
  5. Repeat steps 3 through 5 infinitely.

Again, not infinitely, but do it for a while and Sierpinski’s Triangle with form from the seemingly random points being plotted on the screen. While it’s not truly random because each point is determined by its previous point, the direction of the next point is random making the image materialize over time.

Check it out by loading seirpinski1 from the .d64 image and watch it being created. Here’s the BASIC program with the wedge commands for graphics (The back arrows I use for the wedge translate into underscores in this listing).

100 rem -- triangle fractal --
110 ifpeek(49152)<>76thenprint"ml":load"hires.prg",8,1
120 sys49152
130 dimx%(2),y%(2)
140 poke53280,0
150 x%=0:y%=0:c%=16:z%=0
160 _l:_c:_g
170 x%(0)=160:y%(0)=0
180 x%(1)=0:y%(1)=199
190 x%(2)=319:y%(2)=199
200 a=rnd(-ti)
210 x=160:y=100
220 a=int(rnd(1)*3)
230 x=(x+x%(a))/2:y=(y+y%(a))/2
240 x%=x:y%=y:_p
250 ifpeek(198)=1then270
260 goto220
270 geta$:_t

Lines 170-190 set up the three points of the triangle. Line 220 selects which triangle point we’re going to head towards and line 230 calculates the midpoint of our new location. Line 240 plots the point and then 250 checks to see if we hit any key to end the program cleanly, otherwise go back to 220 to pick the next direction to head towards.

Adding some color.

The other cool thing we can do is because these are points getting plotted and lines being drawn we can see visually how far the point was from the previous point by as assigning a color to that value. on the disk, sierpinski2 has a color version of the program. It is quite a bit slower than the other program, but make a nicer image in the end. I wrote it so the grays and white color are points that moved the least and reds moved the farthest. It makes for an interesting image that shows the relationship of distance traveled to sections of the triangle.

Below is the program for the color version of the program. This version has a bit more commenting for you to follow along. This program also starts by plotting color, but you can toggle that on and off using the “C” key. You’ll notice how much slower the program is when calculating color, but it still makes a nicer image.

100 rem -- sierpinski gasket --
110 ifpeek(49152)<>76thenprint"ml":load"hires.prg",8,1
120 sys49152:rem start graphic wedge
130 rem --set variables--
140 dimx%(2),y%(2),cl%(14)
150 x%=0:y%=0:c%=16:z%=0:cl=2
160 poke53280,0
170 rem --read color data--
180 forn=0to14
190 : readcl%(n)
200 : nextn
210 _l:_c:_g :rem open graphic screen
220 rem --define 1st triangle--
230 x%(0)=160:y%(0)=0
240 x%(1)=0:y%(1)=199
250 x%(2)=319:y%(2)=199
260 a=rnd(-ti) :rem seed random
270 x=160:y=100:rem starting point
280 rem --build sierpinski gasket--
290 : a=int(rnd(1)*3)
300 : x=(x+x%(a))/2:y=(y+y%(a))/2
310 : ifcl>1then350
320 : x%=x:y%=y:_p
330 : ifpeek(198)=1then420
340 : goto290
350 :rem --color routine--
360 : dx=abs(x-xo):dy=abs(y-yo)
370 : dl=sqr(dx*dx+dy*dy)/10
380 : ifdl>14thendl=14
390 : c%=cl%(dl)*16
400 : xo=x:yo=y
410 : goto320
420 rem --key pressed--
430 geta$
440 ifa$="c"thencl=-cl:c%=16:goto290
450 _t:end
460 rem --color data--
470 data 1,15,12,11,6,4,14,3,7,13,5,9,8,2,10

Having an easy way to do graphics on the C64 has given me lots of interesting new things to program. I hope to have more examples of generating fractals in the future to enjoy and share! There are plenty of graphic programming tools out there for the C64 so see what you like and PLAY!

-Michael