Home > database >  Is there a way to choose the direction that you start from when determining the angle between two ve
Is there a way to choose the direction that you start from when determining the angle between two ve

Time:04-20

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!

CodePudding user response:

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

CodePudding user response:

I think that you are looking for something like this:

import numpy as np

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

v3 = np.array([1,0,0])
def find_angle(vec1,vec2):
    #Vec1 is "primary"
    vec1 = vec1
    vec2 = vec2
    angle=np.arccos(np.dot(vec2/np.linalg.norm(vec2),vec1/np.linalg.norm(vec1)))*180/np.pi

    if np.isnan(angle):
        angle = 0
    return angle

def find_angle_from_primary(vec1,vec2):
    #x-axis
    X = np.zeros(len(vec1))
    X[0] = 1
    angle1 = find_angle(vec1,X)
    angle2 = find_angle(vec2,X)
    return np.mod(angle2-angle1,360)

angle = find_angle_from_primary(v1, v2)

print(angle)

Here I use the x-axis as a reference angle.

  • Related