Graphics Programs Reference
In-Depth Information
get a visual comparison of these functions at different angles, go to the Chapter 5 folder
and open sinCosCalculator.swf .
You can use the Right Arrow and Left Arrow keys to turn the angle of the wheel. Along
the horizontal axis is the angle of the wheel. As it is turned, the value of the sine and
cosine functions are plotted along the vertical axis for each angle. The results produce
several items worth noting. Once the angle reaches 360 degrees, the graphs simply
repeat themselves and form a wave pattern, hence the use of the terms sine waves
and cosine waves . These types of waves occur all over the place, and we'll look at a
simple example shortly. Another interesting characteristic of the two waves is that if
we shift the sine wave back 90 degrees or the cosine wave forward 90 degrees, they
are identical.
Let's take a look at the script for the calculator. The starting angle in degrees is initially
set to 0 as usual. In line 3, the variable A for the amplitude is set to 100. The amplitude
is a measure of the maximum height of a wave form. For sound waves, the greater the
amplitude, the louder the sound. Since the maximum value for a sine and cosine wave
is 1, setting the amplitude to 100 scales the results up by a factor of 100 pixels so that
we can see the curves easily.
1
2
3
4
// initialze the starting angle and amplitude
angleInDegrees = 0;
A = 100;
The frame loop begins in line 5, and the first thing done is to test whether the Right
Arrow or Left Arrow keys have been pressed. Here's what happens when the Right
Arrow key has been pressed. First we increase the angle in degrees in line 10. The
sin_mc and cos_mc movie clips are yellow and orange dots located at the bottom of
the screen. These will be duplicated in lines 13 and 14 each time the Right Arrow key is
pressed. The variable k is set to the angle in degrees and used to place each duplicate
on its own level. The variable j is set to be 500 greater than k . This enables the sin_mc
copy to be placed on its own level higher than the cos_mc copy. The value 500 was cho-
sen only because it is a sufficiently big number to avoid any overlapping of levels.
When the Left Arrow key is pressed, we first store the current value of the angle in
degrees in k (line 18). Since we are moving backwards, we need to remove the cur-
rent copies of the sin_mc and cos_mc movie clips (lines 19 and 20). Then we need to
decrease the angle in degrees. After converting the angle in degrees to radians in line
Search WWH ::




Custom Search