Vector Addition

Distances and Lengths


Graphs: sine, cosine and tangent

Circular Functions

Simple Harmonic Motion

Frequency and Amplitude

Adding, Multiplying Waves

Inverse Functions


Introduction to Expressions


Project Index


Inverse Funtions: Arcsine, Arccosine and Arctangent

The inverse functions tell you which angle would produce the value you've specified with the corresponding trigonometric function. For instance, arcsine(n) gives you the angle whose sine equals n.

Because the trigonometric functions are periodic, many different angles will produce the same values. For instance, 0° and 360° both have the same sine (0). The inverse functions will always return the angle closest to 0° that would produce the trigonometric value you've specified.

In Javascript, the inverse functions return angles specified in radians. So in order to use their results with parameters such as rotation or with effect controls, you'll need to convert units with radians_to_degrees().

Like the other trigonometric functions, the inverse functions belong to the Math object. The table below shows how each of these functions is written, the range of values it accepts, and the range of values it can produce, given in both radians and degrees:

Inverse Function Input Range Results, in radians in Degrees
Math.asin(number) -1 to 1 -Pi/2 to Pi/2 -90° to 90°
Math.acos(number) -1 to 1 0 to Pi 0° to 180°
Math.atan(number) -inf to inf -Pi/2 to Pi/2 -90° to 90°
Math.atan2(y, x) -inf to inf, 2D -Pi to Pi -180° to 180°

You cannot pass values larger than 1 or less than -1 to either Math.asin() or Math.acos(), because neither sine nor cosine can ever produce values outside this range. So there's no possible angle the inverse functions could return. If you try to pass a too-large or too-small value to these inverse functions, you'll get the following error dialog, and After Effects will disable your expression:

Although this dialog confusingly suggests that you've tried to divide something by zero, you've really just submitted a value outside the range accepted by Math.asin() or Math.acos(). This won't happen with the arctan functions because tangent returns a range of ±infinity—so any value is acceptable to arctangent.

You may have noticed that there are two arctan functions: Math.atan() and Math.atan2(). The difference is that the first takes a number as its argument, while the second requires two numbers, corresponding to the y and x components of a vector. Also, the first returns values from -Pi/2 radians to Pi/2 radians, whereas the second returns a wider range of -Pi to Pi. In general, you'll want to use this second form, Math.atan2().

Remember that tangent = opposite / adjacent. So if you were to use the number form of arctan, you'd write something like:


Unfortunately, this will fail whenever the adjacent is zero, such as at an angle of 90°, because division by zero is not allowed. So Math.atan() will never return an angle of 90°, and in fact will return a divide-by-zero error (for real this time) whenever you try.

The Math.atan2() form, on the other hand, is specifically designed to avoid these divide-by-zero errors, and will always return correct results.

Keep in mind, by the way, that it expects the arguments to be y-first: Math.atan2(y, x). This is because the function is written to expect y to correspond to the opposite and x to the adjacent, making the tangent equal to y/x. We write the write the numerator first by convention, giving us Math.atan2(y, x).

Example: Point At

Math.atan2() forms the basis of a simple 'point at' expression that will orient a layer or effect control to point towards another layer or point in your comp. First, we'll give the complete expression, then dissect it:


delta=sub(this_point, that_point);
angle=Math.atan2(delta[1], delta[0]);

'This_point' and 'that_point' designate the position to point from and the position to point to, respectively. We calculate the vector between these two positions by subtracting one from the other, and assign the results to a variable 'delta'.

Next, we separate the x- and y-components of delta, and pass them to Math.atan2(), y-first. Finally, we convert the output of this function from radians_to_degrees, and pass the results to whatever parameter we've assigned the expression to.

When applied to rotation, this expression will make the layer's right edge point towards the target. To make the top edge face the target, simply subtract 90° to the results, e.g. 'radians_to_degrees(angle)-90;'.

Here, the 'point at' expression has been applied to the A's rotation parameter.

Click here to download this project file. (Windows users click here.)



Entire contents © 2001 JJ Gifford.