Calculate Angle (Clockwise) Between Two Points

Calculate angle (clockwise) between two points

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 acos
from math import sqrt
from math import pi

def 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 degrees
def 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.

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;

calculate anti clockwise angle between 2 points

Back to basics, without numpy.

atan2 already gives you an anticlockwise angle, but between -180 and 180. You can add 360 and calculate modulo 360 to get an angle between 0 and 360:

from math import atan2, degrees

def anti_clockwise(x,y):
alpha = degrees(atan2(y,x))
return (alpha + 360) % 360

print(anti_clockwise(480, 480))
# 45.0
print(anti_clockwise(-480, -480))
# 225.0

x should just be the difference in X coordinates between the green and red LEDs. Same goes for y.

How to find the clockwise angle between two vectors in python?

Vector geometry provides (at least) two useful formulas for finding the angle between two vectors:

  • the dot product formula

where a · b can be computed using

  • and the cross-product formula:

where

and since our vectors are two dimensional, we can take a3 and b3 (the components in the z-axis direction) equal to 0. This simplifies the formula even further:

|a x b| = |a1 * b2 - a2 * b1| = |a| * |b| * sin(ϴ)

The ϴs is these two formulas, however, have different interpretations.
With the dot product, the angle is the included angle between the two vectors -- and thus always a value between 0 and pi.

With the cross product, the angle is measured in the counterclockwise direction from a to to b. Since you are looking for the angle measured in the clockwise direction, you would simply reverse the sign of the angle obtained using the cross product formula.

In Python, math.asin returns values in the range [-pi/2, pi/2], whereas math.acos returns values in the range [0, pi]. Since you want angles in the range [-pi/2, pi/2] (in radians), the cross-product formula seems to be the more promising candidate:

import math

class Vect:

def __init__(self, a, b):
self.a = a
self.b = b

def findClockwiseAngle(self, other):
# using cross-product formula
return -math.degrees(math.asin((self.a * other.b - self.b * other.a)/(self.length()*other.length())))
# the dot-product formula, left here just for comparison (does not return angles in the desired range)
# return math.degrees(math.acos((self.a * other.a + self.b * other.b)/(self.length()*other.length())))

def length(self):
return math.sqrt(self.a**2 + self.b**2)

vector1 = Vect(2,0)

N = 12
theta = [i * 2 * math.pi / N for i in range(N)]
result = []
for t in theta:
vector2 = Vect(math.cos(t), math.sin(t)) ## a2*i + b2*j
angle = vector1.findClockwiseAngle(vector2)
result.append((math.degrees(t), angle))

print('{:>10}{:>10}'.format('t', 'angle'))
print('\n'.join(['{:>10.2f}{:>10.2f}'.format(*pair) for pair in result]))

prints

     t     angle
0.00 -0.00
30.00 -30.00
60.00 -60.00
90.00 -90.00
120.00 -60.00
150.00 -30.00
180.00 -0.00
210.00 30.00
240.00 60.00
270.00 90.00
300.00 60.00
330.00 30.00

Above, t is the angle from vector1 to vector2 measured in the counterclockwise direction in the range (0, 360) degrees. angle is the angle from vector1 to vector2 measured in the clockwise direction and in the range (-90, 90) degrees.

Find angle between two points

The answers regarding atan2 are correct. For reference, here is atan2 in Scratch block form:

scratch custom block

calculating angle between three points but only anticlockwise in python

Now using angular cosine distance to calculate the angle between two vectors is quite good, but in your case it might be better to use arc tangent as mentioned in the comments.

Now assuming you want to calculate the counterclockwise angle between BCD, you can do this by using the numpy's atan2 function. atan2(x, y) will give the angle between the origin point y to x.

import numpy as np

def calculate_angle(point_a, point_b):
""" Calculate angle between two points """
ang_a = np.arctan2(*point_a[::-1])
ang_b = np.arctan2(*point_b[::-1])
return np.rad2deg((ang_a - ang_b) % (2 * np.pi))

a = np.array([14, 140])
b = np.array([13, 120])
c = np.array([12, 130])
d = np.array([11, 110])

# create vectors
ba = a - b
bc = c - b
cd = d - c

# calculate angle
cosine_angle = np.dot(ba, bc) / (np.linalg.norm(ba) * np.linalg.norm(bc))

angle = np.arccos(cosine_angle)
inner_angle = np.degrees(angle)

print inner_angle # 8.57299836361


# see how changing the direction changes the angle
print calculate_angle(bc, cd) # 188.572998364
print calculate_angle(cd, bc) # 171.427001636

Java: Calculating the angle between two points in degrees

you could add the following:

public float getAngle(Point target) {
float angle = (float) Math.toDegrees(Math.atan2(target.y - y, target.x - x));

if(angle < 0){
angle += 360;
}

return angle;
}

by the way, why do you want to not use a double here?

Find angle between two points, respective to horizontal axis?

Most programming languages/APIs provide a function, atan2(), which finds the angle and takes the quadrant into consideration. Just use that.



Related Topics



Leave a reply



Submit