# Sine and Cosine in C++

Hey i’m one of the programmers from team 245 the Adambots and we are trying to use the gyro and accelerometer together to determine distances traveled with our robot.

Since we are not expert programmers, we are wondering how to use sin and cos to find distances traveled using vectors.

Note: we just need the syntax for the use of these functions.

from
www.cplusplus.com/reference/clibrary/cmath/sin/
and
www.cplusplus.com/reference/clibrary/cmath/cos/

*note: you may need to #include <math.h>

you absolutely have to import math.h, unless you’re using gnulibc which has math.h byitself

Or you can use the C++ wrapper for math.h and do
#include <cmath>

We used the accelerometer to determine the distance travelled by doing a double integration on acceleration. We wrote a generic accelerometer module that will give you accel/vel/distance on all three axes. Then we defined a macro called MAGNITUDE that will take the x and y vectors and gives you distance magnitude and also the macro DIR_DEGREE to calculate the heading.

``````
#define MAGNITUDE(x,y)          sqrt(pow(x, 2) + pow(y, 2))
#define DIR_RADIANS(x,y)        (((x == 0.0) && (y == 0.0))? 0.0: atan2(x, y))