Direct Way of Computing Clockwise Angle Between 2 Vectors

Direct way of computing clockwise angle between 2 vectors

2D case

Just like the dot product is proportional to the cosine of the angle, the determinant is proprortional to its sine. So you can compute the angle like this:

dot = x1*x2 + y1*y2      # dot product between [x1, y1] and [x2, y2]
det = x1*y2 - y1*x2 # determinant
angle = atan2(det, dot) # atan2(y, x) or atan2(sin, cos)

The orientation of this angle matches that of the coordinate system. In a left-handed coordinate system, i.e. x pointing right and y down as is common for computer graphics, this will mean you get a positive sign for clockwise angles. If the orientation of the coordinate system is mathematical with y up, you get counter-clockwise angles as is the convention in mathematics. Changing the order of the inputs will change the sign, so if you are unhappy with the signs just swap the inputs.

3D case

In 3D, two arbitrarily placed vectors define their own axis of rotation, perpendicular to both. That axis of rotation does not come with a fixed orientation, which means that you cannot uniquely fix the direction of the angle of rotation either. One common convention is to let angles be always positive, and to orient the axis in such a way that it fits a positive angle. In this case, the dot product of the normalized vectors is enough to compute angles.

dot = x1*x2 + y1*y2 + z1*z2    #between [x1, y1, z1] and [x2, y2, z2]
lenSq1 = x1*x1 + y1*y1 + z1*z1
lenSq2 = x2*x2 + y2*y2 + z2*z2
angle = acos(dot/sqrt(lenSq1 * lenSq2))

Edit: Note that some comments and alternate answers advise against the use of acos for numeric reasons, in particular if the angles to be measured are small.

Plane embedded in 3D

One special case is the case where your vectors are not placed arbitrarily, but lie within a plane with a known normal vector n. Then the axis of rotation will be in direction n as well, and the orientation of n will fix an orientation for that axis. In this case, you can adapt the 2D computation above, including n into the determinant to make its size 3×3.

dot = x1*x2 + y1*y2 + z1*z2
det = x1*y2*zn + x2*yn*z1 + xn*y1*z2 - z1*y2*xn - z2*yn*x1 - zn*y1*x2
angle = atan2(det, dot)

One condition for this to work is that the normal vector n has unit length. If not, you'll have to normalize it.

As triple product

This determinant could also be expressed as the triple product, as @Excrubulent pointed out in a suggested edit.

det = n · (v1 × v2)

This might be easier to implement in some APIs, and gives a different perspective on what's going on here: The cross product is proportional to the sine of the angle, and will lie perpendicular to the plane, hence be a multiple of n. The dot product will therefore basically measure the length of that vector, but with the correct sign attached to it.

How to find clockwise angle in degree between two vector in java

Thanks to MvG from Direct way of computing clockwise angle between 2 vectors his solution working perfect.

double dot = C.dot(M);
double det = ((C.getX()*M.getZ()) - (C.getZ()*M.getX()));
double angle = Math.toDegrees(Math.atan2(det, dot));

String movedirection = "";
if(angle < -135 || angle >= 135){
movedirection = "Front";
};
if(angle < 135 && angle >= 45){
movedirection = "right";
};
if(angle < 45 && angle >= -45){
movedirection = "back";
};
if(angle < -45 && angle >= -135){
movedirection = "left";
};

Using atan2 to find angle between two vectors

 atan2(vector1.y - vector2.y, vector1.x - vector2.x)

is the angle between the difference vector (connecting vector2 and vector1) and the x-axis,
which is problably not what you meant.

The (directed) angle from vector1 to vector2 can be computed as

angle = atan2(vector2.y, vector2.x) - atan2(vector1.y, vector1.x);

and you may want to normalize it to the range [0, 2 π):

if (angle < 0) { angle += 2 * M_PI; }

or to the range (-π, π]:

if (angle > M_PI)        { angle -= 2 * M_PI; }
else if (angle <= -M_PI) { angle += 2 * M_PI; }

Is there a way to choose the direction that you start from when determining the angle between two vectors?

The trick seemed to be to understand that the orthogonal axis vector is also representative of a plane. Once that is understood, you can solve this problem as below:

import numpy as np
import math


v2 = np.array([0.20297736, -0.19208957, -0.63320655])
v1 = np.array([-0.63721771, 0.17457218, 0.12666251])
axis_vector = np.array([ 0.21708059, 0.95127211, -0.21899175])


def find_angle(v1, v2, vn):
x1 = v1[0]
y1 = v1[1]
z1 = v1[2]

x2 = v2[0]
y2 = v2[1]
z2 = v2[2]

xn = vn[0]
yn = vn[1]
zn = vn[2]

dot = x1 * x2 + y1 * y2 + z1 * z2
det = x1 * y2 * zn + x2 * yn * z1 + xn * y1 * z2 - z1 * y2 * xn - z2 * yn * x1 - zn * y1 * x2
angle = math.atan2(det, dot)*180/np.pi

return angle
angle = find_angle(v1, v2, axis_vector)

This answer was based off of: Direct way of computing clockwise angle between 2 vectors

Edit: For sake of completeness, If you have to do this computation multiple times with multiple vectors and planes.

    def find_angle(v1, v2, vn):
if v1.shape[0] == 1:
x1 = v1[0]
y1 = v1[1]
z1 = v1[2]

x2 = v2[0]
y2 = v2[1]
z2 = v2[2]

xn = vn[0]
yn = vn[1]
zn = vn[2]
dot = x1 * x2 + y1 * y2 + z1 * z2
det = x1 * y2 * zn + x2 * yn * z1 + xn * y1 * z2 - z1 * y2 * xn - z2 * yn * x1 - zn * y1 * x2
angle = m.atan2(det, dot) * 180 / np.pi
angle = np.array([angle])
else:
elementWiseConcat = np.asarray(list((zip(v1, v2, vn))))
dot = np.einsum('ij, ij->i', v1, v2)
det = np.linalg.det(elementWiseConcat)
angle = np.arctan2(det, dot) * 180 / np.pi

return angle

Find clockwise angle between two points with respect to an arbitrary origin

How do I find angle between CA and CB in clockwise direction?

// The atan2 functions return arctan y/x in the interval [−π , +π] radians
double Dir_C_to_A = atan2(Ay - Cy, Ax - Cx);
double Dir_C_to_B = atan2(By - Cy, Bx - Cx);
double Angle_ACB = Dir_C_to_A - Dir_C_to_B;

// Handle wrap around
const double Pi = acos(-1); // or use some π constant
if (Angle_ACB > Pi) Angle_ACB -= 2*Pi;
else if (Angle_ACB < -Pi) Angle_ACB += 2*Pi;

// Answer is in the range of [-pi...pi]
return Angle_ACB;

Efficient way to get the angle between two vectors in a single plane?

Suppose that your two vectors live at u = (x, y1, z) and v = (x, y2, z), and you're interested in the planar angle between the two along the plane spanned by the two vectors. You'd have to compute the dot product and the magnitude, but you can save a few operations:

u.v = x.x + y1.y2 + z.z
u^2 = x.x + y1.y1 + z.z
v^2 = x.x + y2.y2 + z.z

So we should precompute:

float xz = x*x + z*z, y11 = y1*y1, y12 = y1*y2, y22 = y2*y2;

float cosangle = (xz + y12) / sqrt((xz + y11) * (xz + y22));
float angle = acos(cosangle);

Finding Signed Angle Between Vectors

If you have an atan2() function in your math library of choice:

signed_angle = atan2(b.y,b.x) - atan2(a.y,a.x)

How do I get the x or y angle of two vectors?

It depends on what you mean by by X and by Y. If you mean get the angle on an axis of a vector, you will want to use inverse trigonometry, most likely atan2.

This reference is for C++, but the function should work the same way for most languages.

Cant find angle between 2 vectors

If you are developing your program in C++, then to calculating an angle between two vectors you can use the atan2 function, it is present in many programming languages.

You need to call atan2 giving it the two components of a single vector and then you make calculations this way:

Calculating for the first vector: atan2(v1_y, v1_x)

Calculating for the second vector: atan2(v2_y, v2_x)

Caution:
If the value returned by atan2 is negative (as atan2 returns value from range (-pi;+pi]), then you need to add 2 * pi to the result for each of the vector.

Finally you subtract the values of the vectors and what you get is the angle. The angle will be either positive or negative, depending which atan2 value you subtract from which one.

How to compute directional angle between two 2D vectors in MatLab?

To start with, an easier way to think about the angle between two 2D vectors with coordinates is to align an axis with your coordinate vectors and think about the relationship between two vectors. Using the drawing below, we can see that a relative angle can be found by subtracting one angle from the other.

Sample Image

Source: http://almaer.com/blog/uploads/atan2.png

It is not too hard to figure out looking at this graph that we can say

angle = atan2d(y2,x2) - atan2d(y1,x1)

However, since neither of your vectors are known to be aligned along the coordinate axis, the case can arise where the difference above is not in the range (-180, 180). This means we need to code in a check to add or subtract 360 degrees to get our desired angle:

if abs(angle) > 180
angle = angle - 360*sign(angle)
end

Note, you are using a kind of reverse notation (CW positive) so the final code would look like:

v1 = p1 - p2;
x1 = v1(1);
y1 = v1(2);
v2 = p3 - p1;
x2 = v2(1);
y2 = v2(2);
angle = atan2d(y1,x1) - atan2d(y2,x2)

if abs(angle) > 180
angle = angle - 360*sign(angle)
end

Where v1 and v2 have been changed to match your drawing.



Related Topics



Leave a reply



Submit