Inverse Cosine

Started by
6 comments, last by Graham 22 years, 6 months ago
I just wanted to know if there is an inverse cosine, and sine function in the math.h file or anywhere else. thanks
Go on an Intense Rampage
Advertisement
Yes, they''re called "acos" and "asin" which is short for "arc cosine" and "arc sin".

[Resist Windows XP''s Invasive Production Activation Technology!]
In math.h:

double acos( double x );

double asin( double x );

asin, acos, atan... in math.h
Although the acos, asin, and atan functions work, I recommend the atan2 function. This takes two arguments, the rise and run of a right triangle (or alternately, the sin and cos of the angle <- the better way) and returns the proper angle.

This function always returns the correct answer, where the others may not.
Here''s a useful approximation for asin(x)
for small x, -0.5 < x < 0.5

asin(x) =(approx) x * sqrt( 3 / ( 3 - x*x ) )

Useful if you require a fast and not too accurate answer.


Anonymous Poster''s point about using atan2() instead of acos, asin is a good one, though I thought I''d clarify what atan2 does.

When you use just acos or asin, it is impossible to tell which quadrant the angle is in. For example, if you have a cosine of 0.5, the actual angle could lie in the first or fourth quadrants of the plane. The acos() function should return an angle in the first quadrant since it returns an angle from 0 to pi (3.14159265). Similarly, for example if you have a sine of 0.5 the actual angle could lie in the first or second quadrants and asin() should return an angle in the first quadrant since it returns an angle from -pi/2 to pi/2.

The atan2() method returns the correct angle, valued from -pi to pi, in any of the four quadrants. If you need to get the angle in the correct quadrant, atan2() is the function to use. The problem is, you may not have enough information! You may only know the cosine, for example, if you''re doing something with dot products. It may be expensive to get the actual value of the sine of the angle in order to call atan2(). It might be cheaper to use acos or asin and just use some geometric hint to determine the quadrant, then shift the angle.

The parameters of atan2() are the sine and cosine of the angle:

angle = atan2(sine, cosine).

*OR* multiples of sine and cosine:

angle = atan2(C*sine, C*cosine), where C is a constant.

Graham Rhodes
Senior Scientist
Applied Research Associates, Inc.
Graham Rhodes Moderator, Math & Physics forum @ gamedev.net
::bows down to Graham::

Always a pleasure to watch you work.

-------------
-WarMage
...if you''re part of a hive-mind, is it still bad form to sex0r your cousin?...

This topic is closed to new replies.

Advertisement