简体   繁体   中英

atan2() using two vectors

Below is an example of how we can find the angle in all four quadrant wise using the function atan2 .

#include <stdio.h>       
#include <math.h>       

#define PI 3.14159265

int main ()
{
  double x, y, result;
  x = -10.0;
  y = -10.0;
  result = atan2 (y,x);
  printf ("The arc tangent for (x=%f, y=%f) is %f rad\n", x, y, result );
  return 0;
}

在此处输入图像描述

The above case is applicable when we know the x and y value to find the quadrant. What if we know the vector, and use them to find the angle?

For example, we know two vectors. Is there any example function that would take the two vectors, works a similar way to: atan2 (y_value,x_value);

To find angle between vectors in 2D, namely angle needed to rotate the first vector to make it collinear with the second one:

angle = atan2(cross(a, b), dot(a, b))

where cross and dot refer to cross product and dot product of vectors. In components:

angle = atan2(a.x * b.y - a.y * b.x, a.x * b.x + a.y * b.y)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM