Calculate angle (clockwise) between two points Calculate angle (clockwise) between two points python python

Calculate angle (clockwise) between two points


Numpy's arctan2(y, x) will compute the counterclockwise angle (a value in radians between -π and π) between the origin and the point (x, y).

You could do this for your points A and B, then subtract the second angle from the first to get the signed clockwise angular difference. This difference will be between -2π and 2π, so in order to get a positive angle between 0 and 2π you could then take the modulo against 2π. Finally you can convert radians to degrees using np.rad2deg.

import numpy as npdef angle_between(p1, p2):    ang1 = np.arctan2(*p1[::-1])    ang2 = np.arctan2(*p2[::-1])    return np.rad2deg((ang1 - ang2) % (2 * np.pi))

For example:

A = (1, 0)B = (1, -1)print(angle_between(A, B))# 45.print(angle_between(B, A))# 315.

If you don't want to use numpy, you could use math.atan2 in place of np.arctan2, and use math.degrees (or just multiply by 180 / math.pi) in order to convert from radians to degrees. One advantage of the numpy version is that you can also pass two (2, ...) arrays for p1 and p2 in order to compute the angles between multiple pairs of points in a vectorized way.


Use the inner product and the determinant of the two vectors. This is really what you should understand if you want to understand how this works. You'll need to know/read about vector math to understand.

See: https://en.wikipedia.org/wiki/Dot_product and https://en.wikipedia.org/wiki/Determinant

from math import acosfrom math import sqrtfrom math import pidef length(v):    return sqrt(v[0]**2+v[1]**2)def dot_product(v,w):   return v[0]*w[0]+v[1]*w[1]def determinant(v,w):   return v[0]*w[1]-v[1]*w[0]def inner_angle(v,w):   cosx=dot_product(v,w)/(length(v)*length(w))   rad=acos(cosx) # in radians   return rad*180/pi # returns degreesdef angle_clockwise(A, B):    inner=inner_angle(A,B)    det = determinant(A,B)    if det<0: #this is a property of the det. If the det < 0 then B is clockwise of A        return inner    else: # if the det > 0 then A is immediately clockwise of B        return 360-inner

In the determinant computation, you're concatenating the two vectors to form a 2 x 2 matrix, for which you're computing the determinant.


Here's a solution that doesn't require cmath.

import mathclass Vector:    def __init__(self, x, y):        self.x = x        self.y = yv1 = Vector(0, 1)v2 = Vector(0, -1)v1_theta = math.atan2(v1.y, v1.x)v2_theta = math.atan2(v2.y, v2.x)r = (v2_theta - v1_theta) * (180.0 / math.pi)if r < 0:    r += 360.0print r