0

I'm having some trouble with calculating the angle between 2 vectors with the same starting point - means there are 3 coordinates,

I have tried to define this func but it doesnt work when giving more then 1 vector:

def AngleBtw2Points(pointA, pointB):
    ang1 = np.arctan2(*pointA[::-1])
    ang2 = np.arctan2(*pointB[::-1])
    return np.rad2deg((ang1 - ang2) % (2 * np.pi))

picture with example of the issue

The input is (X0,Y0) (X1,Y1) (X2,Y2)

Cardstdani
  • 4,999
  • 3
  • 12
  • 31
Snorko
  • 43
  • 4
  • https://en.wikipedia.org/wiki/Dot_product – sehan2 Aug 01 '21 at 11:53
  • Does this answer your question? [Angles between two n-dimensional vectors in Python](https://stackoverflow.com/questions/2827393/angles-between-two-n-dimensional-vectors-in-python) – sehan2 Aug 01 '21 at 11:59
  • Does this answer your question? [Using atan2 to find angle between two vectors](https://stackoverflow.com/questions/21483999/using-atan2-to-find-angle-between-two-vectors) – yann ziselman Aug 01 '21 at 12:02
  • was managed to fix it, thank you. my function is: def AngleBtw2Points(pointDES, pointSRC, pointA): ang = math.degrees(math.atan2(pointA[1]-pointSRC[1], pointA[0]-pointSRC[0]) - math.atan2(pointDES[1]-pointSRC[1], pointDES[0]-pointSRC[0])) return ang + 360 if ang < 0 else ang – Snorko Aug 01 '21 at 12:08

2 Answers2

0

As noted in the comment, you can use the dot product fairly easily to get the angle between two vectors.

def AngleBtw2Vectors(vecA, vecB):
    unitVecA = vecA / np.linalg.norm(vecA)
    unitVecB = vecB / np.linalg.norm(vecB)
    return np.arccos(np.dot(unitVecA, unitVecB))
Austen
  • 418
  • 6
  • 11
0
def AngleBtw2Points(pointDES, pointSRC, pointA):
    ang = math.degrees(math.atan2(pointA[1]-pointSRC[1], pointA[0]-pointSRC[0]) - math.atan2(pointDES[1]-pointSRC[1], pointDES[0]-pointSRC[0]))
    return ang + 360 if ang < 0 else ang

This fixed my issue, thank you for you help :)

Snorko
  • 43
  • 4
  • Not the best way... It is enough to use only one call of atan2 (look at @yann ziselman comment) – MBo Aug 01 '21 at 12:37