Apple II

Sine Wave

How do I do a horizontally scrolling sine wave in Applesoft BASIC?

Here is a Sine Wave Demo written in Applesoft BASIC.

sine-wave.bas.txt

0 GOTO 1000

REM POKE16
1 H = INT (B / 256) : B = B - H * 256 : GOSUB 2 : B = H

REM POKE8
2 POKE A,B : A = A + 1 : B = 0 : Z = 0 : R(0) = 0 : R(1) = 0 : R(2) = 0 : K(0) = 0 : K(1) = 0 : RETURN

REM ADD SHAPE VECTOR
3 ON Z = 2 AND (K = 1 OR R = 0) GOSUB 4 : K(Z) = K : R(Z) = R : Z = Z + 1 : IF Z < 3 THEN RETURN

REM ADD SHAPE BYTE
4 B = R(2) * 64 + K(1) * 32 + R(1) * 8 + K(0) * 4 + R(0) : ON B > 0 GOTO 2 : B = 192: GOSUB 2 : RH = R:KH = K:R = 1:K = 0: GOSUB 3 : R = RH : K = KH : RETURN

REM https://www.applefritter.com/content/how-do-i-do-horizontally-scrolling-sine-wave-applesoft-basic
5 HGR
10 HCOLOR=3
20 FOR X=0 TO 278
30 LET Y=INT(-65*SIN(X*3.14/28))+70
40 HPLOT X,Y
50 NEXT X
60 GOTO 5

REM CREATE 1 SHAPE
70 B = 1
80 GOSUB 1

REM OFFSET TO SHAPE
90 B = 4
100 GOSUB 1

170 HGR
180 HCOLOR= 3
190 IF S = 1 THEN VTAB 22: PRINT "CREATING SHAPE ... ";
200 P = FN Y(279)
210 FOR X = 0 TO 279 STEP S
215 IF S < 1 THEN HCOLOR= C * 4 + 3:C = C = 0
220 Y = FN Y(X)
230 D = Y - P
240 P = Y
250 FOR I = 1 TO ABS (D)
260 HPLOT X,V + 70
265 IF S = 1 THEN K = 1:R = SGN (D) + 1
267 IF S = 1 THEN ON I > 1 OR ABS (D) = 0 GOSUB 3
270 IF I > 1 THEN V = V + SGN (D)
280 NEXT I
281 IF S = 1 THEN IF D THEN R = 1: GOSUB 3
290 NEXT X
295 IF S < 1 THEN WAIT 49152,128: GOTO 510
300 IF S THEN IF Z THEN K = 0:R = 1: GOSUB 3: GOTO 300
310 IF S THEN B = 0: GOSUB 2

320 SCALE= 1:IF FNP(O)<>1THENB = 1: GOSUB 1:B = 4: GOSUB 1:B=53:GOSUB2:B=39:GOSUB2:GOSUB2:SCALE=16
330 ROT= 0
340 Y = 70
350 P = 230: REM PAGE
360 M = 280: REM MAXIUMUM RIGHTMOST
370 F = 49236: REM SHOW PAGE 1
380 G = 32: REM DRAW PAGE 1
390 V = 49237: REM SHOW PAGE 2
400 W = 64: REM DRAW PAGE 2
410 POKE P,W: HCOLOR= 0: HPLOT 0,0: CALL 62454
420 X = 0
430 XDRAW S AT X,Y
440 J = PEEK (V) + PEEK (49234) + PEEK (49239) + PEEK (49232): HOME
450 POKE P,G: HCOLOR= 0: HPLOT 0,0: CALL 62454
460 H = 279
470 XDRAW S AT H,Y
480 J = PEEK (F)
490 Z = 2:K = 49152:E = 127
500 FOR Q = 0 TO S STEP 0: POKE P,W: XDRAW S AT X,Y:X = X - Z + (X = 0) * M: XDRAW S AT X,Y:J = PEEK (V): POKE P,G: XDRAW S AT H,Y:H = H - Z + (H = S) * M: XDRAW 1 AT H,Y:J = PEEK (F):Q = PEEK (K) > E: NEXT
510 J = PEEK (49168)
520 TEXT
530 IF A$ = "C" THEN PRINT " BSAVE SHAPE,A"O",L"A - O;
540 VTAB 23
550 END

REM BLOAD SHAPE
800 PRINT CHR$ (4)"BLOADSHAPE"
810 GOTO 320

REM DRAW SINE WAVE
900 S = .5
910 GOTO 170

REM ANIMATE RECTANGLE
920 POKE 24576,0
930 GOTO 320

1000 HIMEM: 8192
1010 DEF FN Y(X) = INT ( - 65 * SIN (X * 3.1415926535 / 28))
REM PEEK16
1020 DEF FN P(A) = PEEK (A) + PEEK (A + 1) * 256
REM $6000 SHAPE TABLE
1030 LET O = 24576
REM $00E8 HGR SHAPE POINTER
1040 LET A = 232
1050 LET B = O
1060 GOSUB 1
1070 LET A = O
1080 LET S = 1
1090 LET M$=CHR$(13)
1100 TEXT
1110 HOME
1120 PRINT "SINE WAVE DEMO"M$M$
1130 PRINT "A)NIMATE"M$
1140 PRINT "B)LOAD SHAPE"M$
1150 PRINT "C)REATE SHAPE"M$
1160 PRINT "D)RAW SINE WAVE 560 X 192"M$
1170 PRINT "E)XIT"M$
1180 PRINT "CHOOSE A, B, C, D, OR E >";
1190 FOR Q = 0 TO S
1200     GET A$
1210     K = ASC(A$)
1220     K = K - 32 * (K>95)
1230 IF K < 65 OR K > 73 THEN NEXT
1240 PRINT A$
1250 ON K - 64 GOTO 320,800,70,900,1260,1260,920,5
1260 END