Issue
I have two 3-d vectors originating from the origin with
v1 = array([ 0.20297736, -0.19208957, -0.63320655])
v2 = array([-0.63721771, 0.17457218, 0.12666251])
These two vectors are orthogonal to the vector axis_vector = array([ 0.21708059, 0.95127211, -0.21899175])
I am trying to determine the angle between v1 and v2 with the condition that I always start from v1. This means that V1 will be the point at which 0-degrees exists. Moving counterclockwise from v1, I want to determine the angle between v1 and v2.
Currently, I have been using the following:
angle=np.arccos(np.dot(vec2,vec1)/(np.linalg.norm(vec1)*np.linalg.norm(vec2))) *180/np.pi
but this particular line of code does not let me dictate which vector takes priority as the starting vector. As a result, it always returns the same angle without regard for which vector I wish to start from.
Any help would be appreciated!
Solution
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
Answered By - user2852630
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.