Drawing a Spiral in BASIC
A while ago I came across this short snippet of BASIC code, originally written for QuickBASIC 4.5:
x = 320
y = 240
r = 100
c = 1
PSET (x, y - r), 0
FOR a = 0 TO 72 STEP .01
LINE -(x + r * SIN(a), y - r * COS(a)), c
r = r - .01
c = RND(1) * 15
This uses simple geometry to draw a circle, but slowly reduces the radius so that it ends up being a spiral.
I thought it might be a fun, quick exercise to port that to good, old Atari BASIC. Here it is:
10 GRAPHICS 15+16
50 PLOT X,Y-R
80 FOR A=0 TO SPIRALS*TWOPI STEP 0.01
110 DRAWTO NX,NY
130 IF LASTNX<X AND NX>X THEN C=C+1:IF C>3 THEN C=1
140 COLOR C
160 NEXT A
180 DUR=(TIME2-TIME1)/60/60:REM MINUTES
190 GOTO 190
As you can see, line number are needed, of course. Atari BASIC has some good built-in graphics drawing commands so the PSET becomes PLOT to set draw the starting point and the LINE command becomes DRAWTO to keep drawing the edges of the spiral.
I used Atari Graphics mode 15 (available on XL/XE machines), which has 4 colors (3 without the background), but its resolution is only 160x192 so the pixels are not square. This required tweaking the starting values. I also chose to only switch to a new color when each new spiral is started.
This is a little bit longer than the QuickBASIC version, but I did split things out onto multiple lines to make it more understandable.
This actually takes a while to run in Atari BASIC, but it is a lot faster in BASIC XE with FAST mode on (times below).
After it finishes you can press BREAK and then print DUR to see how long it took in in minutes:
With BASIC XE it took about 6.3 minutes. With Atari BASIC it took 21.7 minutes.
Join the free Goto 10 email list to get notified of posts as soon as they are published.
Here is the result:
You should see this on a Commodore 64. Its BASIC has no drawing commands built-in, so this would have to be done with a lot of POKEs and GOSUBs, making for some really ugly code.
Nice! I saw the post on FB Atari, and just inspired me to write my own version in PL65 for the Atari 800. It completes in about 23s. Pretty cool, I think I'll upload my version to AtariAge, if anyone is interested.
Oh you know what, I'm going to post the code here - because it also occurs to me that it may be an optimization technique for other languages too. PL65 forces the use of unsigned integer arithmetic, which is why it's fast. You could also do the same in Atari basic - it's just not required. This is what the code looks like in PL65:
EDIT AGAIN NEVERMIND, not enough space to post it...oh well, it's on atariage.
I tried it in Turbo Basic. I had to change TIME1 and TIME2 to T1 and T2, since TB has a TIME command. I got 6.1 mins.