Previous Section  < Day Day Up >  Next Section

Using Math Libraries in C++

As you start programming, you'll find that it's not necessary to reinvent the wheel. Many functions have already been written a thousand times, so why rewrite them? The trigonometric functions have already been mastered, and they're out there to be used. If you're coding in C++, you can include the math.h library, which already contains function calls for sine, cosine, tangent, and their inverses.

NOTE

Whenever you want to use these function calls, make sure you include math.h at the beginning of your code by using the following line:


#include <math.h>


The sine, cosine, and tangent functions take in a double and output a double. The input, or parameter, is an angle expressed in radians, and the output is a decimal number between –1 and 1. Remember that the angle must be in radians, so you might need to perform a last-minute conversion from degrees to radians (see the first section of this chapter). Quite often, programmers first define a constant for p:

p = 3.14159265

and then use it later to convert from degrees to radians. A typical sine function call looks like this:


result = sin (angle*pi/180);

Once again, perhaps the best way to create a constant for repeated use is the #define the value in the top of a frequently used header file.


#define PI 3.14159265f

#define RadToDeg 57.29577951f

#define DegToRad 0.017453293f

The cosine and tangent functions use the exact same format.

math.h Trig Functions

sine: double sin (double x)

cosine: double cos (double x)

tangent: double tan (double x)

return a number between –1 and 1 when an angle in radians is input.


The inverse trig functions are also defined in math.h. Remember that the inverses are used to return the angle (see the second section of this chapter). These three functions also take in a double and return the angle in radians as a double. Again, you can always convert back to degrees if you want. The functions are actually labeled asin, acos, and atan.

math.h Inverse Trig Functions

arcsine: double asin (double x)

arccosine: double acos (double x)

arctangent: double atan (double x)

return an angle in radians when a double value (x) is input.


A typical line of code looks like this:


angle = asin (input)*180/pi;

NOTE

Don't forget to include the math.h library just like before:


#include <math.h>

You can also include the more frequently used <cmath> library, because it's a bit more compatible.

Also note that there are restrictions on the input for asin and acos; they are defined only for values between –1 and 1.


TIP

Division is a more expensive operation than multiplication, so you might want to consider defining another constant equal to 1/p (0.31830989) and then multiply instead of divide.


NOTE

Now we have truly set the stage to deal with vectors in future chapters. In this section you tackled the function calls in C++ for the trigonometric functions and their inverses. This is where all the previous sections start to come together in terms of programming. Now you can move forward with their applications in game programming.


    Previous Section  < Day Day Up >  Next Section