
Page 3 of 4
Knowing the length of any two sides of a right triangle is enough information to calculate the other two sides of the right triangle. This is useful when you need to find the cosine, sine, and tangent of a triangle. So, why do you need to find the cosine, sine, and tangent of a triangle. The answer is simple, to calculate points around a circle. If you have not figured it out yet getting the cosine and sine of a point will give the location in radians of a point on a circle in flash. Next you will see why this is useful, I am sure you can image why.
Before I go on, consider what purpose the flash math functions below (in dark gray box) would serve you as you work with angles, triangles, cosine, and sine. Also review the relationship of sine, cosine, and tangent in the image below.
THIS IS VERY IMPORTANT: Most likely you will know the lengths of the opposite and adjacent sides of the triangle because they represent the y and x coordinates of a point. See if you can make sense of this by studying the image below.
| ||
| Math.Atan2(y, x);........calculates the angle (in radians) from the x-axis to a point on the y-axis. |
All right, here it is, if we know the angle of a right triangle and we use the math functions in flash actionscript cos( ) and sin( ) we can calculate the circular path around a circle. Above I showed how to get the angle of an object on the flash stage. For now we will just pick at random angle of 45 degrees. Given this angle we calculate the coordinates where an angle will intersect with a circle. This mean that we we will be a calculate a circular path around a central point or origin.
IMPORTANT: Remember that radius is the distance from the center of a circle to the edge of the circle. And that cosine represents the horizontal (x) and sine the vertical (y) value of the Cartesian coordinate system
Your goal is to find what the x and y coordinate are for the center of the orange circle below.
Remember that you must first change you angle into radians because we are talking about a 45 degree angle, but flash sees it in radians not degrees. The following code is an example of actionscript that will convert the angle into radians and then by using the math functions cos( ) and sin ( ) return the (x, y) coordinates of the orange circle below.
var degrees=45;
radians = degrees* (Math.PI/180);
xcoordinate = radius * cos(radians);
ycoordinate = radius * sin(radians);
The following code is placed on a movie clip in flash call "ball" with a graphic of a orange ball on its timeline. The only other movie clip in the flash file is a clip called "center_cross_hairs". The clip "center_cross_hairs" is used to reference a point or origin in the center of the movie. If you don't know why this is review the start of this tutorial. If I did not use the "center_cross_hairs" clip flash would think (0,0) is located in the top left-hand corner. Remember to name your movie clips in the flash environment for reference in actionscript.


