Sierpinski Curves
- Type
- Graphics
- Category
- Recursive fractal curve demo
- Author
- Allan Sharpe
- Published in
- Antic, February 1988 (Vol. 6, No. 10), "Page 6 Exchange"; first published in Page 6 magazine, May/June 1987
- Year
- 1988
- Language
- Atari BASIC
- Requirements
- Any Atari 8-bit, 16K RAM, Atari BASIC
- Tags
- antic, page-6, fractals, recursion, graphics-7, sierpinski, demo, atari-basic
About the program
In 1987 Antic agreed an exchange with Page 6, then England's leading Atari magazine, and began reprinting the best short programs from it. Sierpinski Curves, by Allan Sharpe of the Brighton users' group, was the second of them. It had first appeared in Page 6's May/June 1987 issue. It draws a closed, square-ish fractal curve four times, each at a finer level of detail and in a new colour, layered on top of one another on a full-screen GRAPHICS 7 display. Then it rubs them all out again, level by level.
Antic's introduction explained fractal curves as "search and replace" drawing. Start with a simple shape, find every corner of a certain kind, replace it with a more detailed shape, and repeat on the new corners. It also passed on Benoit Mandelbrot's claim that Gustave Eiffel used shapes like the Sierpinski "arrowhead" when designing his tower.
Typing it in
There are no special characters, just 54 short lines of arithmetic and GOSUBs. The four subroutines at 200, 300, 400 and 500 look almost the same, but they differ in their directions (A= and B=) and in which subroutines they call. A single swapped number spoils the curve, so check them carefully. The listing in the magazine was called CURVE.BAS, and the disk file is SIERPIN.BAS.
How to use it
Just RUN it. Each of the four levels is drawn in turn, cycling through colours 1–3. Then the program goes through the same four levels with COLOR 0, erasing them, and ends at the READY prompt. It takes a while in Atari BASIC, and watching the curve snake around the screen is half the fun.
How it works
The curve is defined recursively. To draw it at level n, you draw four sides, and each side is made of copies of the curve's pieces at level n−1, joined by short diagonal or straight strokes. The four routines at 200–500 are those four pieces. Each one calls itself and its neighbours, with A and B giving the x and y length of each connecting stroke. The pattern is the classic structured-programming example of mutual recursion.
Atari BASIC can GOSUB recursively, but it has no local variables, so every call would overwrite the level counter of the call that made it. Sharpe gets round this with his own stack. ST() is a small array and SP points to its top. Line 600 pushes a new level before a recursive call, and line 700 pops it afterwards to restore the caller's level, TP. The real recursion depth is only 4, which is why DIM ST(5) is enough.
Line 800 does all the drawing: PLOT X,Y:DRAWTO X+A,Y+B, then it moves the pen. Line 100 works out the starting point and stroke size H for each level, halving H each time so that every level fills the same area.
Variations to try
- Draw a fifth level: change
FOR Z=1 TO 4in line 50 toTO 5andDIM ST(5)toST(6). In GRAPHICS 7 the strokes are then only a pixel or two long, so try GRAPHICS 8+16 withCOLOR 1and a largerH0. - To keep the finished picture on screen, change line 70 to
70 GOTO 70. The erasing pass is skipped, and the program sits there until you press BREAK.
Listing
4 REM SIERPINSKI CURVES
8 REM BY ALLAN SHARPE
11 REM FROM PAGE 6 MAGAZINE
15 DIM ST(5):F=0
45 GRAPHICS 7+16:POKE 708,56
50 FOR Z=1 TO 4
55 C=C+1:IF C>3 THEN C=1
56 COLOR C
57 IF F=1 THEN COLOR 0
61 GOSUB 100
62 NEXT Z
70 IF F=0 THEN COLOR 0:F=1:GOTO 50
90 END
100 H0=96:H=H0/4:X=3.3*H:Y=3*H:I=0
110 I=I+1:X=X-H:H=H/2:Y=Y+H
120 IF I<Z THEN GOTO 110
130 PS=I:GOSUB 600
140 GOSUB 200:A=H:B=-H:GOSUB 800
150 GOSUB 300:A=-H:B=-H:GOSUB 800
160 GOSUB 400:A=-H:B=H:GOSUB 800
170 GOSUB 500:A=H:B=H:GOSUB 800
180 GOSUB 700:RETURN
200 IF TP<=0 THEN RETURN
220 PS=TP-1:GOSUB 600
230 GOSUB 200:A=H:B=-H:GOSUB 800
240 GOSUB 300:A=2*H:B=0:GOSUB 800
250 GOSUB 500:A=H:B=H:GOSUB 800
260 GOSUB 200
270 GOSUB 700:RETURN
300 IF TP<=0 THEN RETURN
320 PS=TP-1:GOSUB 600
330 GOSUB 300:A=-H:B=-H:GOSUB 800
340 GOSUB 400:A=0:B=-2*H:GOSUB 800
350 GOSUB 200:A=H:B=-H:GOSUB 800
360 GOSUB 300
370 GOSUB 700:RETURN
400 IF TP<=0 THEN RETURN
420 PS=TP-1:GOSUB 600
430 GOSUB 400:A=-H:B=H:GOSUB 800
440 GOSUB 500:A=-2*H:B=0:GOSUB 800
450 GOSUB 300:A=-H:B=-H:GOSUB 800
460 GOSUB 400
470 GOSUB 700:RETURN
500 IF TP<=0 THEN RETURN
520 PS=TP-1:GOSUB 600
530 GOSUB 500:A=H:B=H:GOSUB 800
540 GOSUB 200:A=0:B=2*H:GOSUB 800
550 GOSUB 400:A=-H:B=H:GOSUB 800
560 GOSUB 500
570 GOSUB 700:RETURN
600 SP=SP+1:ST(SP)=PS:TP=PS:RETURN
700 SP=SP-1:TP=ST(SP):RETURN
800 PLOT X,Y:DRAWTO X+A,Y+B
820 X=X+A:Y=Y+B:RETURN
Download
The original program from the Antic disk, to LOAD or ENTER instead of typing it: SIERPIN.BAS.
Credits
- Program listing: Sierpinski Curves — program listing (Antic, February 1988 (Vol. 6, No. 10), "Page 6 Exchange"; first published in Page 6 magazine, May/June 1987) — Allan Sharpe
- Picture: Level 1 (pink) and level 2 (green) are finished, and level 3 (blue) is being drawn. — Allan Sharpe (program); screenshot by the site
- Antic, February 1988 - Vol. 6 No. 10 — article: Sierpinski Curves, graphics demo by Allan Sharpe — Allan Sharpe, 1988
- Antic Software Archive — February 1988 - Vol. 6 No. 10 disk: SIERPIN.BAS — AtariMagazines.com (Kevin Savetz), 1988
