1

Firstly, I would like to ask is the angle between to vectors measured clockwise or anticlockwise direction? Can you specify?

I need to calculate the angle between to vectors that I will create from 3 points. I need the angle to be between 0 and 360 degrees and always measured in the same direction.

How do I do this?

I will use this to find the angle required to make it a right angle... perpendicular. The reason direction is important is that i will be doing this twice from the same origin point on vector perpendicular at 90 the other perpendicular at 270. So if it only measures smallest angle i will not know whether i need to add or subtract an angle.

measure the angle between vector 0-1 and 0-3

measure the angle between vector 0-1 and 0-2

0-2 need to 90 degrees anticlockwise to 0-1

0-3 need to be 270 degrees anticlockwise to 0-1 (90 degrees clockwise)

I know the order of the points. 2 always needs to be 90 CCW and 3 needs to be 270 CCW but the points could be anywhere.

Hope this makes sense and many thanks in advance.

Tried to draw a picture but wont let me up load due to "reputation"

willgosling
  • 23
  • 2
  • 6
  • 2
    This has already been asked so many times... anyway, have a look at [the atan2 function](http://www.cplusplus.com/reference/cmath/atan2/), and at [this question for the 0-360 degrees part](http://stackoverflow.com/questions/1311049/how-to-map-atan2-to-degrees-0-360) – Cimbali Jan 06 '15 at 17:45
  • 1
    In cartesian coordinates, angles are typically measured anticlockwise (0 degrees pointing along the positive X axis, 90 degrees along the positive (up) Y-axis) – Alnitak Jan 06 '15 at 17:45
  • The right hand rule determines sign. If you rotate the x-axis counterclockwise into the y-axis, the z-axis points out of the plane towards you. – duffymo Jan 06 '15 at 17:53

1 Answers1

2

First, angles are measured anticlockwise.

Then, if you have two vectors, calculating the angle is:

const double pi = std::atan(1.0) * 4;

struct vect2D {  // of course you could use vectors if you prefer
    double x; 
    double y; 
    double operator*(vect2D& e) { return x*e.x + y*e.y; }  // scalar product of 2 vectors 
    double dist() { return sqrt(x*x + y*y); }  // length
};

double prd_vect(vect2D& u, vect2D& v) 
{   
    return u.x*v.y - u.y*v.x;
}
double angle_rad(vect2D& u, vect2D& v) 
{
   return (prd_vect(u,v)<0 ? -1:1) * acos(u*v / (u.dist()*v.dist())); 
}
double angle_deg(vect2D& u, vect2D& v) 
{
   return angle_rad(u,v) / pi * 180 ; 
}

Demo on how to use it:

vect2D u{ 1, 0 }, v{ -1, 0 }; 
cout << " angle in degrees : " << angle_deg(u,v) << endl; 

Additional remark: two non nul vectors v and w are orthogonal (+90° or -90°) if their scalar product is 0.

So if you have a given vector v and have a fixed point M and a desired length it should be relatively easy with some basic math and the formulas above to compute the position of N such that MN is orthogonal to v.

Christophe
  • 68,716
  • 7
  • 72
  • 138