• Please review our updated Terms and Rules here

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

DistantStar001

Experienced Member
Joined
May 8, 2019
Messages
178
I tweaked this program from something I saw on the 8-bit Guy. Managed to get the wave to fill most of the screen, repeat, and end where it begins on the screen. However, I was wondering if I could get it to scroll horizontally across the screen? or draw from right to left? I don't know, I'm just playing with BASIC and wanted some pointers.



Program listing:

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
 
The answer is yes!

However, the question is - how long does it actually take to draw the sine wave now?

To draw from right to left you would change line 20 to "FOR X=278 to 0 STEP -1" or, wherever you currently have "X", change it to "(278-X)" in lines 30 and 40.

The variable X is the horizontal part of the coordinate (derived in line 20) and Y is the vertical part of the coordinate (derived in line 30). The actual point is plotted in line 40 of course.

To make the wave 'dynamic' you will need to find a technique to avoid as much screen flicker as possible.

One technique is to erase each 'old' point on the horizontal axis (starting at X=0) and plot the new point (at X=0) as though it was (X=1) repeat for the whole screen. The trick is to keep track of where you are starting from at a coordinate of X=0.

Have a think...

Dave
 
Thanks!

Maybe I can set my bar a little lower to achieve a similar effect? Since "HPLOT X,0 TO X,191" draws a vertical line, is it possible to make that line thicker and black, and get it to move across my screen through the sine wave? Essentially, give the illusion that the computer is continuously redrawing the wave? Or am I getting into sprite territory that the Apple II doesn't have?

Even if it only fills the screen, I could set another wave to draw through it. Whipe the screen white, then draw a black wave, whipe black, white wave, repeat?
 
Well

You could try

10 HGR
20 FOR X = 1 to 277
29 HCOLOR = 2
30 HPLOT X + 2,0 TO X + 2,150
31 HCOLOR = 0
32 HPLOT X,0 TO X,150
33 LET Y=INT(-65*SIN(X*3.14/28 ))+70
35 HCOLOR = 3
36 HPLOT X,Y
40 NEXT X
50 GOTO 15

This draws and deletes a line in front of the wave plot. It does draw the wave from the same place each time so you would have to do something with Y if you want it to start in a different place each time so

10 HGR
11 LET A = 0
20 FOR X = 0 to 277
29 HCOLOR = 2
30 HPLOT X + 2,0 TO X + 2,150
31 HCOLOR = 0
32 HPLOT X,0 TO X,150
33 LET Y=INT(-65*SIN((X-A)*3.14/28 ))+70
35 HCOLOR = 3
36 HPLOT X,Y
40 NEXT X
45 A = INT(RND(4)*50)
50 GOTO 20

to give a random x axis offset

Its not fast and the line blinks a bit but it works. You can set line 30 to X+3 and line 20 to 0 TO 276 to make the line blink less but then it leaves a line at the end of the screen (which could be overwritten by a small amount of code after line 40)

Have a play :)

*EDIT*
Just noticed you dont want to see the line, so just set the line colour in 29 to black as well.
Also, I missed out line 32 first time, now amended.
 
Last edited:
I would recommend exploring the possibility of using both graphics displays. Draw the wave on the first display, draw the wave on the second display one unit over, switch displays, erase the wave from the first display and draw a new wave on it two units over, then swap the displays and repeat the process between the two displays.
 
If willing to do a small amount of machine language, you could shift the screen left 1 block of, I think, 7 pixels (8?), and then just draw the last few pixels.

I don't know if there's a block move built in to the Apple ROM. If there is, you may be able to call that from BASIC rather than having to write your own.
 
Back
Top