Sine Waves

Type
Graphics
Category
GRAPHICS 8 line-pattern generator
Author
Patrick Bass
Published in
Antic, October 1985 (Vol. 4, No. 6), BASIC-to-C conversion article
Year
1985
Language
Atari BASIC
Requirements
Any Atari 8-bit computer with enough memory for GRAPHICS 8 (16K or more), Atari BASIC
GRAPHICS 8 pattern of many straight lines joining a sine curve drawn left to right with a cosine curve drawn right to left, forming twisted ribbon shapes

About the program

Patrick Bass had been a game designer and programmer at Atari in its heyday. By the autumn of 1985 he was Antic's new ST Program Editor, hired (as he told it) the day after he'd been turned down as overqualified to deliver pizzas. One of his first articles showed 8-bit owners that C on the new 520ST could look much like BASIC. To make the point he wrote the same small program in both languages, and this is the Atari BASIC version.

He described it as a simple program he'd "used many times" that "creates an impressive graphic display". It plots a sine wave from left to right and a cosine wave from right to left, and joins each pair of points with a line. The heights, periods and step size are all random, so the cascade of lines is different every time.

Typing it in

The listing is plain Atari BASIC with no special characters, and short enough to type in a few minutes. Antic printed TYPO II checksums beside each line of the magazine listing. They are not part of the program. The variable names are long (SINEAMPLITUDE, COSINEPERIOD), and Atari BASIC is happy with them.

How to use it

RUN it. When a pattern is finished, press any key for another. Press a key while one is being drawn and it stops cleanly and waits. Press X (upper case) to quit.

How it works

  • Line 120 is a trick Bass pointed out. FOR LOOP=0 TO 1 STEP 0 never ends by itself, because the counter never changes, so it acts like a REPEAT…UNTIL. Line 270 ends it by setting LOOP=2 when you press X.
  • Lines 150–190 pick an amplitude of 10–80 pixels for each wave, a period of 5–75 (smaller periods give more "hills and valleys") and a step size of 1–6 pixels.
  • Lines 210–220 work out the two Y values. SIN and COS give answers between −1 and 1, so after multiplying by the amplitude, half the screen height (YRES/2) is added to keep the points on screen.
  • Line 230 plots the sine point at XPOINT and draws to the cosine point at XRES-XPOINT. Because the two waves travel in opposite directions, the lines twist into ribbons and fans.
  • Line 240 checks location 764 for a key press and, if there is one, sets XPOINT to its end value so that the loop finishes normally.

Variations to try

Add SETCOLOR 1,0,14 after line 140 for brighter lines, switch to GRAPHICS 7+16 (with XRES=159:YRES=95 in line 110) and change COLOR as you go for coloured patterns, or replace COS with SIN for symmetrical designs.

Listing

10 REM SINE WAVES
20 REM BY PATRICK BASS
30 REM (c) 1985, ANTIC PUBLISHING
100 OPEN #1,4,0,"K:"
110 XRES=319:YRES=191
120 FOR LOOP=0 TO 1 STEP 0
130 GRAPHICS 8+16
140 SETCOLOR 2,0,0:SETCOLOR 4,0,0:COLOR 1
150 SINEAMPLITUDE=(RND(9)*70)+10
160 COSINEAMPLITUDE=(RND(9)*70)+10
170 SINEPERIOD=(RND(9)*70)+5
180 COSINEPERIOD=(RND(9)*70)+5
190 SIZE=(RND(9)*5)+1
200 FOR XPOINT=0 TO XRES STEP SIZE
210 SINEY=(SIN(XPOINT/SINEPERIOD)*SINEAMPLITUDE)+(YRES/2)
220 COSINEY=(COS(XPOINT/COSINEPERIOD)*COSINEAMPLITUDE)+(YRES/2)
230 PLOT XPOINT,SINEY:DRAWTO XRES-XPOINT,COSINEY
240 IF PEEK(764)<>255 THEN XPOINT=XRES
250 NEXT XPOINT
260 GET #1,KEYPRESS
270 IF CHR$(KEYPRESS)="X" THEN LOOP=2
280 NEXT LOOP

Download

The original program from the Antic disk, to LOAD or ENTER instead of typing it: SINEWAVE.BAS.

Credits