Name
Math.atan2( ) Method — determine an angle based on a point
Availability
Flash 5; may be used when exporting Flash 4 movies
Synopsis
Math.atan2(y, x)
Arguments
- y
The y-coordinate of the point.
- x
The x-coordinate of the point.
Returns
The angle, in radians, of the point (x
,
y
) from the center of a circle, measured
counterclockwise from the circle’s positive horizontal axis
(i.e., the X-axis). Ranges from π to -π. (Negative values
indicate angles below the X-axis).
Description
The atan2()
method, like atan(
)
, performs an arc tangent calculation but uses x- and
y-coordinates, rather than their ratio, as arguments. That is,
calculating an arc tangent with atan2( )
as:
Math.atan2(9, 3); // Yields 1.24904577239825
is equivalent to calculating the arc tangent with atan(
)
, using the ratio of 9/3 (or 3), as follows:
Math.atan(3); // Same thing
Usage
Note that the y -coordinate is passed as the
first argument to atan2( )
, whereas the
x-coordinate is passed as the second argument.
This is intentional and required. It mirrors the structure of
tangent, which is the ratio of the side opposite an angle (y) divided
by the side adjacent to the angle (x).
Example
The atan2( )
method can be used to make a
movie
clip point toward a moving target. The
following example, available at the online Code Depot, shows code
that rotates the current clip toward the mouse pointer. It can be
used to orient an enemy spaceship toward a player’s spaceship:
// Rotate movie clip toward mouse onClipEvent (load) { // Convert radians ...
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.