• ### What is your GameDev Story?

#### Archived

This topic is now archived and is closed to further replies.

# Inverse Cosine

This topic is 6326 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I just wanted to know if there is an inverse cosine, and sine function in the math.h file or anywhere else. thanks

##### Share on other sites
In math.h:

double acos( double x );

double asin( double x );

##### Share on other sites
asin, acos, atan... in math.h

##### Share on other sites
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.

##### Share on other sites
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.

##### Share on other sites
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.

##### Share on other sites
::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?...

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 10
• 11
• 13
• 9
• 11
• ### Forum Statistics

• Total Topics
634088
• Total Posts
3015453
×