Sine Wave

This file is part of Timex Sinclair Public Domain Library Tape 1003 . Download the collection to get this file.
Date: 198x
Type: Program
Platform(s): TS 1000
Tags: Demo

This program plots a sine curve on the screen using the low-resolution PLOT command. It precomputes 64 sample points of a full sine wave cycle into array C, scaling the values to span a vertical range centred around y=22 with an amplitude of 20 pixels. After plotting, it calls a subroutine at line 1000 that busy-loops until a key is pressed before returning, effectively pausing execution twice. Six blank PRINT statements then scroll the display downward.


Program Analysis

Program Structure

The program is divided into three logical phases:

  1. Initialisation (lines 10–40): Allocates a 64-element array C and fills it with precomputed sine values.
  2. Plotting (lines 100–145): Iterates over the array, PLOTs each point, waits for two keypresses via subroutine, then scrolls the display with blank PRINTs.
  3. Keypress wait subroutine (lines 1000–1010): Busy-loops on INKEY$ until any key is held down, then RETURNs.

Sine Wave Calculation

Line 30 computes each sample as:

C(V) = 22 + 20 * SIN((V-1)/32 * PI)

With V running from 1 to 64, the argument (V-1)/32 * PI sweeps from 0 to just under 2π, producing one complete cycle. The vertical centre is at pixel row 22 and the amplitude is ±20 pixels, giving a total excursion of 40 pixels. Precomputing into array C avoids recalculating the sine for each plot, which is a sensible optimisation given the expense of trigonometric evaluation.

Plotting Loop

Lines 100–115 step through all 64 array entries and call PLOT G-1, C(G), offsetting G by 1 so x coordinates run 0–63. The curve therefore occupies the left quarter of the 256-pixel-wide display. No DRAW is used between points, so the curve is rendered as discrete dots rather than a connected line; at 64 samples this is generally dense enough to appear continuous.

Keypress Wait Subroutine

The subroutine at lines 1000–1010 implements a busy-wait loop:

  • Line 1000 checks INKEY$; if it is not empty (a key is pressed), it RETURNs immediately.
  • Line 1010 loops back to 1000, spinning until a keypress is detected.

This subroutine is called twice in succession (lines 117 and 125), requiring two separate keypresses before execution continues. This is a common idiom for requiring a deliberate key-press-and-release cycle, since a single call might return on the same keypress that triggered any previous action.

Notable Techniques

  • Precomputing into a DIM array separates data preparation from rendering, a good structural practice even in short BASIC programs.
  • The double GOSUB 1000 ensures the user must press a key once to acknowledge and once to proceed, guarding against accidental continuation.
  • The six blank PRINT statements (lines 135–145) scroll any text area output without clearing the graphics area, providing visual separation.

Anomalies and Dead Code

LineIssue
2000STOP is never reached; execution halts within the keypress loop or falls through the blank PRINTs into an implicit end.
2020RUN after SAVE would restart the program, but this line is only reached as part of the save sequence and is not part of the normal runtime flow.

Content

Appears On

Assembled by Tim Ward from many sources. Contains programs 10122 – 10175.

Related Products

Related Articles

Related Content

Image Gallery

Sine Wave

Source Code

   1 REM "SIN CURV"
  10 DIM C(64)
  20 FOR V=1 TO 64
  30 LET C(V)=22+20*(SIN ((V-1)/32*PI))
  40 NEXT V
 100 FOR G=1 TO 64
 110 PLOT G-1,C(G)
 115 NEXT G
 117 GOSUB 1000
 125 GOSUB 1000
 135 FOR H=1 TO 6
 140 PRINT 
 145 NEXT H
\n1000 IF INKEY$<>"" THEN RETURN 
\n1010 GOTO 1000
\n2000 STOP 
\n2010 SAVE "1016%3"
\n2020 RUN 

Note: Type-in program listings on this website use ZMAKEBAS notation for graphics characters.

People

No people associated with this content.

Scroll to Top