Circles, Polygons and Curves in Atari BASIC

GRAPHICS 8 screen with two concentric circles, a hexagon, a solid white disc and a four-petal rose curve; the text window reports 115 jiffies for a 10-degree circle and 1153 for a 1-degree circle

Atari BASIC has PLOT, DRAWTO and a FILL command, but nothing for circles. That is less of a gap than it seems: a circle is a few lines of trigonometry, and the same loop draws polygons, spirals and flower-like "rose" curves. This guide covers the two classic methods, measures what step size costs, and collects the tricks that magazine programmers used to make curves faster and prettier.

What you'll learn: circles from SIN and COS, filled discs from Pythagoras, polygons as "coarse circles", polar curves such as roses and spirals, the speed/quality trade-off in numbers, and ways to go faster.

What you need: Atari BASIC. The program was run in atari800, and the timings below are from that run.

1. The program

10 REM CIRCLES AND CURVES IN GRAPHICS 8
20 GRAPHICS 8:SETCOLOR 2,0,0:SETCOLOR 1,0,14:COLOR 1:DEG
30 REM 1. CIRCLE FROM SIN AND COS, JOINED WITH DRAWTO
40 CX=50:CY=50:R=40:S=10:GOSUB 500:T1=T
50 REM 2. THE SAME WITH ONE-DEGREE STEPS
60 CX=50:CY=50:R=30:S=1:GOSUB 500:T2=T
70 REM 3. A HEXAGON IS A CIRCLE WITH 60-DEGREE STEPS
80 CX=150:CY=50:R=40:S=60:GOSUB 500
90 REM 4. FILLED DISC FROM X*X+Y*Y=R*R, ONE VERTICAL LINE PER X
100 R=35:FOR X=-R TO R:Y=SQR(R*R-X*X):PLOT 250+X,50-Y:DRAWTO 250+X,50+Y:NEXT X
110 REM 5. A FOUR-PETAL ROSE: R=SIN(2*ANGLE)
120 FOR A=0 TO 360 STEP 3:RR=45*SIN(2*A):X=160+RR*COS(A):Y=118+RR*SIN(A)
130 IF A=0 THEN PLOT X,Y
140 DRAWTO X,Y:NEXT A
150 ? "10-DEGREE CIRCLE: ";T1;" JIFFIES"
160 ? "1-DEGREE CIRCLE: ";T2;" JIFFIES"
170 POKE 77,0:GOTO 170
500 REM CIRCLE CENTRE CX,CY RADIUS R, ANGLE STEP S; TIME IN T
510 T0=PEEK(19)*256+PEEK(20)
520 PLOT CX+R,CY:FOR A=S TO 360 STEP S:DRAWTO CX+R*COS(A),CY+R*SIN(A):NEXT A
530 T=PEEK(19)*256+PEEK(20)-T0:RETURN

2. Circles from SIN and COS

A point on a circle of radius R around (CX, CY) is at

X = CX + R * COS(A)
Y = CY + R * SIN(A)

for every angle A from 0 to 360 degrees. The subroutine at line 500 PLOTs the starting point at angle 0 and DRAWTOs each following point. DEG (line 20) makes SIN and COS take degrees instead of radians, which is easier to think about. Because DRAWTO joins the points with straight lines, the circle is really a polygon with 360/S sides, and that is the speed trade-off:

Step S Sides Time measured
10° 36 115 jiffies (about 2 seconds)
1° 360 1,153 jiffies (about 19 seconds)

Ten times the points takes ten times as long, because the time goes almost entirely on SIN, COS and DRAWTO for each point. At 36 sides a circle of radius 40 already looks round. Henrique Veludo, plotting polar functions in COMPUTE!'s First Book of Atari, recommended exactly this compromise. A very small step is accurate but very slow, a large one fast but rough, and a 1-degree step is "a good compromise" for intricate curves. Measure for yourself with the system clock, as lines 510 and 530 do (locations 19 and 20 count jiffies).

3. Polygons are coarse circles

Take the circle routine and make the step large: 120° gives a triangle, 90° a square, 60° a hexagon (line 80), 45° an octagon. Veludo lists exactly these settings for drawing polygons with his polar plotter. To rotate the polygon, start the angle somewhere other than 0.

4. Filled discs from Pythagoras

For a solid circle, use X² + Y² = R² instead. For each column X from −R to R, the disc extends Y = SQR(RR − XX) above and below the centre, so one vertical DRAWTO per column fills it (line 100). David Diamond used this method in COMPUTE!'s First Book of Atari Graphics, with a twist: drawing only every second column (STEP 2) in GRAPHICS 8 leaves alternate pixel columns lit. On an NTSC TV that produces solid artifact colours instead of white, and starting on an odd or even column chooses which colour (see NTSC Artifacting). Emulators show this only with artifacting switched on.

R*R rather than R^2 is deliberate. Diamond points out that multiplying is faster than raising to a power.

5. Polar curves: roses, spirals and hearts

The circle routine is a special case of a polar curve, where the radius depends on the angle:

X = CX + R(A) * COS(A)
Y = CY + R(A) * SIN(A)

Line 120 uses R = 45 × SIN(2A), which gives a four-petal rose. Other functions from Veludo's list (scaled to fit the screen):

R(A) Shape
constant circle (or polygon with a big step)
A (angle in radians) spiral
2(1 + COS A) cardioid (heart shape)
SIN(3A) three-petal rose
COS(3 SIN A) and similar nested forms more intricate rosettes

Spirals need several turns, so let A run past 360 degrees. Line 130 PLOTs the first point and later points are joined with DRAWTO, so the curve is continuous. Keep every point on the screen: in GRAPHICS 8 with a text window, Y must stay within 0–159. Veludo's program checked the range and stopped instead of risking ERROR 141.

6. Going faster

  • Use fewer points. As the timings show, the step size dominates. Use the largest step that still looks smooth at the size you're drawing.
  • Use symmetry. A circle is the same in all eight octants. Compute one eighth and plot eight mirrored points, which saves seven-eighths of the SIN and COS calls when plotting dots.
  • Precompute SIN and COS into arrays once (for example every 10 degrees), then reuse them for every circle. Only the multiplications by R remain.
  • Avoid repeated work inside the loop: CX+R*COS(A) is fine, but keep anything that doesn't depend on A outside the loop.
  • Move to machine language or a faster BASIC for animation. Turbo-BASIC XL, FastBasic or a small USR routine run the same algorithm many times faster.

7. Aspect ratio

Atari pixels aren't square in most modes. Each mode has its own pixel width and height, and the exact proportions depend on the TV standard. If circles look like eggs on your display, scale one axis: multiply the Y offset by a correction factor (somewhat less or more than 1) and adjust it by eye on your screen. In the example, GRAPHICS 8 circles looked round in the emulator without correction.

Troubleshooting

  • ERROR 141 (cursor out of range): a point fell off the screen. Check the centre and radius against the mode's limits, or add a range check.
  • Circles drawn as stars or scribbles: the angles were in radians when you meant degrees. Add DEG.
  • Gaps or a straight line from the edge: the first point was DRAWn instead of PLOTted, so the line started from the previous cursor position.
  • Coloured fringes on a real TV: NTSC artifacting of single-pixel lines in GRAPHICS 8. Use GRAPHICS 7 or thicker lines for clean colour.

Further reading

  • Henrique Veludo, "Graphics of Polar Functions", COMPUTE!'s First Book of Atari (1981).
  • David Diamond, "GRAPHICS 8 in Four Colors Using Artifacts", COMPUTE!'s First Book of Atari Graphics.
  • On this site: Graphics in Atari BASIC: Modes, COLOR, SETCOLOR, PLOT, DRAWTO and FILL, NTSC Artifacting, Making Atari BASIC Faster (and Smaller).

Credits