Goto 10 RetroComputing: Atari and more

Share this post

Drawing a Spiral in BASIC

www.goto10retro.com

Drawing a Spiral in BASIC

Paul Lefebvre
Dec 2, 2022
5
5
Share this post

Drawing a Spiral in BASIC

www.goto10retro.com

A while ago I came across this short snippet of BASIC code, originally written for QuickBASIC 4.5:

CLS
RANDOMIZE (TIMER)
x = 320
y = 240
r = 100
c = 1
SCREEN 12
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
NEXT

This uses simple geometry to draw a circle, but slowly reduces the radius so that it ends up being a spiral.

Sinus und Kosinus am Einheitskreis 1.svg

I thought it might be a fun, quick exercise to port that to good, old Atari BASIC. Here it is:

10 GRAPHICS 15+16
20 C=1
30 TIME1=PEEK(20)+PEEK(19)*256+PEEK(18)*65536
40 X=80:Y=96:R=96
50 PLOT X,Y-R
60 TWOPI=3.14*2
70 SPIRALS=11
80 FOR A=0 TO SPIRALS*TWOPI STEP 0.01
90 NX=X+R/2*SIN(A)
100 NY=Y-R*COS(A)
110 DRAWTO NX,NY
120 R=R-0.01
130 IF LASTNX<X AND NX>X THEN C=C+1:IF C>3 THEN C=1
140 COLOR C
150 LASTNX=NX
160 NEXT A
170 TIME2=PEEK(20)+PEEK(19)*256+PEEK(18)*65536
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.

1

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:

? DUR

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:

1

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.

5
Share this post

Drawing a Spiral in BASIC

www.goto10retro.com
Previous
Next
5 Comments
Rolly
Dec 3, 2022·edited Dec 3, 2022Liked by Paul Lefebvre

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.

Expand full comment
Reply
Mark Miller
Dec 2, 2022Liked by Paul Lefebvre

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.

Expand full comment
Reply
2 replies by Paul Lefebvre and others
3 more comments…
TopNewCommunity

No posts

Ready for more?

© 2023 Paul Lefebvre
Privacy ∙ Terms ∙ Collection notice
Start WritingGet the app
Substack is the home for great writing