Name

Math.cos( ) Method — compute the cosine of an angle

Availability

Flash 5; may be used when exporting Flash 4 movies

Synopsis

Math.cos(theta)

Arguments

theta

An angle, in radians (not degrees), in the range 0 to 2π.

Returns

The cosine of theta (the result is in the range -1.0 to 1.0).

Description

The cos( ) function returns the trigonometric cosine of an angle. In a right triangle, the cosine of an angle is the result of dividing the length of the side adjacent to the angle by the triangle’s hypotenuse.

Usage

Note that cos( ) expects angles to be provided in radians, not degrees.

Example

trace (cos(0));            // Displays: 1.0
trace (cos(Math.PI));      // Displays: -1.0

The cos( ) function can be used along with sin( ) to calculate a point on a circle, which we use in the following example to move a movie clip in a circular path. Given the radius of a circle, r, and an angle, θ, measured counterclockwise from the positive horizontal axis, a point’s location is (r*cosθ, r*sinθ):

                     // CODE ON FRAME 1
var radius = 100;             // Radius of circle path
var centerX = 275;            // Horizontal center of circle path
var centerY = 200;            // Vertical center of circle path
var rotAngleDeg = 0;          // Angle of object in degrees, measured 
                              //   counterclockwise from the horizon (x-axis)
var rotAngRad;                // Radian version of rotAngleDeg

// Convert degrees to radians. There are 2*pi radians per 360 degrees.
function degreesToRadians(degrees) {
  return (degrees/180) * Math.PI;
}

// CODE ON FRAME 2 // Increase the rotation angle by 5 degrees rotAngleDeg ...

Get ActionScript: The Definitive Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.