This program draws a geometric spiral pattern on screen using trigonometric rotation and scaling. It initialises cosine and sine values for two rotation angles — 60° (PI/3) for the inner hexagonal step and 5° (PI/36) for the outer spiral progression — then iterates 43 spiral arms each composed of 6 line segments drawn with PLOT and DRAW. A scaling factor of 0.95 (sf) shrinks the radius slightly on each outer iteration, producing the inward spiral effect. An aspect-ratio correction factor (sc=1.16) compensates for the non-square pixel geometry of the display.
Program Analysis
Program Structure
The program is a straightforward iterative graphics routine with no subroutines or user input. After initialisation (lines 20–110), it enters a double loop: the outer FOR j loop (line 120) runs 43 times for each spiral arm, and the inner FOR i loop (line 130) runs 7 times (0 to 6) to draw 6 line segments per arm. After all drawing is done, execution ends at STOP on line 280.
Variable Initialisation
| Variable | Value | Purpose |
|---|---|---|
c | COS(PI/3) | Cosine of 60° — inner rotation step |
s | SIN(PI/3) | Sine of 60° — inner rotation step |
c1 | COS(PI/36) | Cosine of 5° — outer spiral progression |
s1 | SIN(PI/36) | Sine of 5° — outer spiral progression |
sf | 0.95 | Scale factor per outer iteration (shrinks radius) |
x, y | 95, 0 | Initial vector coordinates |
cx, cy | 130, 88 | Screen centre offset |
sc | 1.16 | Horizontal aspect-ratio correction |
Drawing Mechanism
Within the inner loop, screen coordinates sx and sy are computed by applying the aspect-ratio scale to x and adding the centre offsets. On the first pass (i=0), the branch at line 160 skips the draw call and simply records the starting point into sx1/sy1. On subsequent passes, PLOT sx1,sy1 followed by DRAW (sx-sx1),(sy-sy1) draws a relative line segment to the new point. This PLOT-then-DRAW idiom is a standard Sinclair BASIC technique for connecting successive coordinate pairs.
Rotation Mathematics
The 2D rotation matrix is applied explicitly each iteration. Lines 200–220 rotate the vector (x,y) by 60° using the standard formulae:
xn = x·cos θ − y·sin θy = x·sin θ + y·cos θx = xn(temporary variable avoids overwritingxbefore it is used)
The same pattern is repeated at lines 240–260 for the 5° outer rotation, with the additional multiplication by the scale factor sf to progressively reduce the spiral radius.
Notable Techniques
- Pre-computed trig values: All cosine and sine values are computed once before the loops, avoiding repeated and expensive floating-point trig calls inside the tight double loop.
- Aspect-ratio correction: The value
sc=1.16applied only to thexcoordinate compensates for the rectangular pixel shape of the display, keeping the spiral visually circular. - Temporary variable for rotation: The use of
xnas a scratch variable when computing the rotation ensures the old value ofxis preserved for theycalculation, a necessary correctness technique. - Multiple statements on one line: Line 190 uses the colon separator to store both
sx1andsy1in one logical line, a common BASIC compactness idiom.
Potential Anomalies
The inner loop runs FOR i=0 TO 6, which is 7 iterations, but only 6 actual line segments are drawn (the first iteration at i=0 merely initialises the starting point). This is intentional: it produces a closed or near-closed hexagonal step per arm. The choice of 43 outer iterations combined with a 5° rotation step gives 43 × 5° = 215° of total outer rotation, meaning the spiral does not complete a full revolution, which appears to be a deliberate aesthetic choice inherited from the original Apple II program.
Content
Source Code
1 REM "SPIRAL"
2 REM By Joe E. Jenkins 3100 Mockingbird Amarillo, TX 79109 11 NOV 85
3 REM a conversion by Ted Knyszek from Apple II to TS-2068 See Creative Computing Feb 1984
20 LET c=COS (PI/3)
30 LET s=SIN (PI/3)
40 LET c1=COS (PI/36)
50 LET s1=SIN (PI/36)
60 LET sf=.95
70 LET x=95
80 LET y=0
90 LET cx=130
100 LET cy=88
110 LET sc=1.16
120 FOR j=1 TO 43
130 FOR i=0 TO 6
140 LET sx=x*sc+cx
150 LET sy=cy+y
160 IF i=0 THEN GO TO 190
170 PLOT sx1,sy1
180 DRAW (sx-sx1),(sy-sy1)
190 LET sx1=sx: LET sy1=sy
200 LET xn=x*c-y*s
210 LET y=x*s+y*c
220 LET x=xn
230 NEXT i
240 LET xn=sf*(x*c1-y*s1)
250 LET y=sf*(x*s1+y*c1)
260 LET x=xn
270 NEXT j
280 STOP
Note: Type-in program listings on this website use ZMAKEBAS notation for graphics characters.
