freeman69@gmx.com
Because SIN and COS are so useful, it's worth looking at these commands in more detail.
So far we've used them to draw circles and to move in a particular direction (other than vertically and horizontally). They can also be used to find the angle between two lines and to rotate points about an origin, but that's for later.
Mathematically, the sine and cosine of an angle are defined as the ratio of the length of the sides of a right-angled triangle to the length of the hypotenuse. Pythagoras comes in handy, knowing that the square on the hypotenuse is equal to the sum of the squares on the other two sides.
If this is unfamiliar, then don't worry. Some mathematical tools are short cuts; the product of years of work, handed down over the ages. Sometimes we'll just make use of a convenient mathematical tool because it's there and not worry about why it works.
The following program demonstrates the relationship between SIN & COS.
It also shows the much simpler relationship between radians and degrees. As mentioned previously, computers tend to work in radians, while humans often find degrees more convenient. There are 360 degrees in a circle, or 2 PI radians (approximately equal to 2 x 3.14 = 6.28 radians). Working with values between zero and 360 is easier for most of us than using values between zero and 6.28 something.
10 MODE 9:OFF
20 DIM xaxis$(4):xaxis$()="0","#/2","#","3#/2","2#"
30 VDU 23,35,0,0,127,34,34,34,34,0
40 r=100
50 VDU 23,23,2;0;0;0;:REM line thickness=2
60
70 VDU 5
80 ORIGIN 240,512+256
90 MOVE -96,r+32:PRINT"SIN"
100 MOVE 0,-32:VDU 5:PRINT"Degrees"
110 LINE 0,0,0,r:LINE 0,0,360*2,0
120 FOR a=0 TO 360 STEP 90
130 LINE a*2,0,a*2,-4
140 MOVE a*2,0:PRINT;a
150 NEXT
160 GCOL 0,2
170 FOR a=-90 TO 360+90
180 PLOT a*2,SIN(RAD(a))*r
190 NEXT
200 GCOL 0,7
210
220 ORIGIN 240,512
230 MOVE -96,r+32:PRINT"COS"
240 MOVE 0,-32:PRINT"Radians"
250 LINE 0,0,0,r:LINE 0,0,360*2,0
260 FOR a=0 TO 360 STEP 90
270 LINE a*2,0,a*2,-4
280 MOVE a*2,0:PRINT xaxis$(a DIV 90)
290 NEXT
300 GCOL 0,3
310 FOR a=-PI/2 TO 5*PI/2 STEP 0.01
320 PLOT DEG(a)*2,COS(a)*r
330 NEXT
340 GCOL 0,7
350 VDU 4
360 PRINTTAB(14,22);"Hold SPACE to pause"
370
380 ORIGIN 240,256
390 FOR a=0 TO 360
400 PLOT SIN(RAD(a))*(r+8),COS(RAD(a))*(r+8)
410 NEXT
420
430 a=0:first=TRUE
440 REPEAT
450 TIME=0
460 IF NOT first THEN
470 GCOL 3,7
480 LINE a*2,512-r,a*2,512+r
490 LINE a*2,256-r,a*2,256+r
500 GCOL 0,0
510 LINE 0,0,SIN(RAD(a))*r,COS(RAD(a))*r
520 LINE 0,0,SIN(RAD(a))*r,0
530 LINE 0,0,0,COS(RAD(a))*r
540 ENDIF
550
560 IF NOT INKEY(-99) a+=1:IF a>=360 a-=360
570
580 GCOL 3,7
590 LINE a*2,512-r,a*2,512+r
600 LINE a*2,256-r,a*2,256+r
610 GCOL 0,2:LINE 0,0,SIN(RAD(a))*r,0
620 GCOL 0,3:LINE 0,0,0,COS(RAD(a))*r
630 GCOL 0,7:LINE 0,0,SIN(RAD(a))*r,COS(RAD(a))*r:first=FALSE
640 WAIT 4-TIME
650 *FX 21
660 UNTIL FALSE
670 END
SIN & COS: Code explained...
10 MODE 9:OFF
20 DIM xaxis$(4):xaxis$()="0","#/2","#","3#/2","2#"
30 VDU 23,35,0,0,127,34,34,34,34,0
'xaxis$' is a string array, or list, containing 5 rows (zero to 4). Line 20 populates this array with specific values that are used to label one of the two graphs. Note the hash symbol (ASCII 35) will be replaced by our user-defined character for the Greek letter PI defined on line 30.
40 r=100
50 VDU 23,23,2;0;0;0;:REM line thickness=2
'r' is the radius of our reference circle. The y-axis of both graphs show values between -r and +r.
Line 50 is a special VDU command that changes the thickness of graphical lines. Make sure you enter the commas and semi-colons correctly!
70 VDU 5
80 ORIGIN 240,512+256
90 MOVE -96,r+32:PRINT"SIN"
100 MOVE 0,-32:VDU 5:PRINT"Degrees"
110 LINE 0,0,0,r:LINE 0,0,360*2,0
120 FOR a=0 TO 360 STEP 90
130 LINE a*2,0,a*2,-4
140 MOVE a*2,0:PRINT;a
150 NEXT
160 GCOL 0,2
170 FOR a=-90 TO 360+90
180 PLOT a*2,SIN(RAD(a))*r
190 NEXT
200 GCOL 0,7
The above draws and labels the top graph, for displaying the sine function. The x-axis of this graph shows degrees. The x-axis of the lower graph shows radians, but remember that the two are interchangeable.
220 ORIGIN 240,512
230 MOVE -96,r+32:PRINT"COS"
240 MOVE 0,-32:PRINT"Radians"
250 LINE 0,0,0,r:LINE 0,0,360*2,0
260 FOR a=0 TO 360 STEP 90
270 LINE a*2,0,a*2,-4
280 MOVE a*2,0:PRINT xaxis$(a DIV 90)
290 NEXT
300 GCOL 0,3
310 FOR a=-PI/2 TO 5*PI/2 STEP 0.01
320 PLOT DEG(a)*2,COS(a)*r
330 NEXT
340 GCOL 0,7
350 VDU 4
The lower graph displays the cosine function. Both SIN and COS return values between -1 (minus one) and 1 (plus one).
360 PRINTTAB(14,22);"Hold SPACE to pause"
380 ORIGIN 240,256
390 FOR a=0 TO 360
400 PLOT SIN(RAD(a))*(r+8),COS(RAD(a))*(r+8)
410 NEXT
Lines 380 to 410 draw the reference circle
430 a=0:first=TRUE
440 REPEAT
450 TIME=0
460 IF NOT first THEN
470 GCOL 3,7
480 LINE a*2,512-r,a*2,512+r
490 LINE a*2,256-r,a*2,256+r
500 GCOL 0,0
510 LINE 0,0,SIN(RAD(a))*r,COS(RAD(a))*r
520 LINE 0,0,SIN(RAD(a))*r,0
530 LINE 0,0,0,COS(RAD(a))*r
540 ENDIF
The first part of our main loop deletes the lines drawn within the reference circle. We're using the exclusive-or function to draw and delete these lines.
560 IF NOT INKEY(-99) a+=1:IF a>=360 a-=360
If the user holds the SPACE bar then the animation pauses.
580 GCOL 3,7
590 LINE a*2,512-r,a*2,512+r
600 LINE a*2,256-r,a*2,256+r
610 GCOL 0,2:LINE 0,0,SIN(RAD(a))*r,0
620 GCOL 0,3:LINE 0,0,0,COS(RAD(a))*r
630 GCOL 0,7:LINE 0,0,SIN(RAD(a))*r,COS(RAD(a))*r:first=FALSE
640 WAIT 4-TIME
650 *FX 21
660 UNTIL FALSE
670 END
Two moving lines are drawn, one on each graph, cycling between zero and 360 degrees.
Three lines are drawn within the circle: the white radial line sweeping through 360 degrees, the x-component of the tip of the radial arm and the y-component of the tip of the radial arm.