
Now lets learn how to create the sine wave in the computer using qbasic . It is preety easy to create a sine wave in qbasic so lets get started
< Step 1 /> : create a loop
create a loop starting from 0 to 360 and define screen as we are playing with graphics .
cls
screen 13
for i = o to 360
next i
< Step 2 /> : convert the numbers into radians and use the SIN function.
you have to convert numbers into radians as SIN function requires the angle argument to be in radians. the formula is :
(x/180)*pie
where x is angle and value of pie = 3.1415
so we can write like this :
cls
screen 13
for i = o to 360
x= sin((i/180)*3.1415)
next i
<Step 3/> : using pset function to draw it on screen.
the last thing is you use pset function to get it drawn on screen and it goes like this.
cls
screen 13
for i = o to 360
x= sin((i/180)*3.1415)
pset(i,(x*50)+50),4
next i
ok now lets look at pset . while using pset function x is multiplied and added with 50 . it is the way of defining the amplitude. if you set it to zero then you would just get a straight line so you have to define the amplitude.while using pset instead of i you can use (i/360)*320 because we used screen 13 and it is just 320 pixels whereas sine wave is 360 pixels .
here is the final program :
cls
screen 13
for i = o to 360
x= sin((i/180)*3.1415)
pset((i/360)*320,(x*50)+50),4
next i
Thank you so much,just what I was looking for. When I was a kid (circa 87) I would program a scrolling zig zag on my Radio Shack (Tandy) Color Computer but I always wanted to know how to make a wave like the cool folks did.
ReplyDeleteAfter 30 years,I finally did it.so thank you :)
QB64 code
CLS
start:
FOR r = 0 TO 360
x = SIN((r / 180) * (3.1415))
PRINT SPACE$((x * 35) + 38); "x"; (i)
FOR g = 1 TO 300
delay
NEXT g
r = r + 10
NEXT r
GOTO start
SUB delay:
FOR d = 1 TO 10000
NEXT d
END SUB