I have studied the dot product from vector analysis in my school. Now that formula, I will use for finding the angle between three points. We have use multiple dimentional data like 1D, 2D, 3D and higher dimensions not only 2D. But i explained with 2D data points.
The dot product may be defined algebraically or geometrically. The geometric definition is based on the notions of angle and distance [magnitude of vectors]. The equivalence of these two definitions relies on having a Cartesian coordinate system for Euclidean space.
Geometric definition:
geometric object that possesses both a magnitude and a direction. A vector can be pictured as an arrow. Its magnitude is its length, and its direction is the direction that the arrow points to. The magnitude of a vector a is denoted by ||a||. The dot product of two Euclidean vectors a and b is defined by. where θ is the angle between a and b.
Explanation:
Coding[Python]:
Using math library:
Using numpy library:
I have write down a code to calculate angle between three points using their 3D coordinates.
import numpy as np
a = np.array[[32.49, -39.96,-3.86]]
b = np.array[[31.39, -39.28, -4.66]]
c = np.array[[31.14, -38.09,-4.49]]
f = a-b # normalization of vectors
e = b-c # normalization of vectors
angle = dot[f, e] # calculates dot product
print degrees[cos[angle]] # calculated angle in radians to degree
output of the code:
degree 33.4118214995
but when i used one of the software to calculate the same it gives output bit different 120 degree. please help
reference i have used to write the program:
[How to calculate bond angle in protein db file?]
Mr_and_Mrs_D
30.2k37 gold badges173 silver badges348 bronze badges
asked Feb 3, 2016 at 11:58
3
Your original code is pretty close. Adomas.m's answer is not very idiomatic numpy:
import numpy as np
a = np.array[[32.49, -39.96,-3.86]]
b = np.array[[31.39, -39.28, -4.66]]
c = np.array[[31.14, -38.09,-4.49]]
ba = a - b
bc = c - b
cosine_angle = np.dot[ba, bc] / [np.linalg.norm[ba] * np.linalg.norm[bc]]
angle = np.arccos[cosine_angle]
print np.degrees[angle]
answered Feb 3, 2016 at 13:50
EricEric
92.4k52 gold badges230 silver badges359 bronze badges
2
I guess numpy is quite enough:
from numpy import *
from numpy.linalg import norm
a = array[[32.49, -39.96,-3.86]]
b = array[[31.39, -39.28, -4.66]]
c = array[[31.14, -38.09,-4.49]]
f = b-a
e = b-c
abVec = norm[f]
bcVec = norm[e]
abNorm = f / abVec;
bcNorm = e / bcVec;
res = abNorm[0] * bcNorm[0] + abNorm[1] * bcNorm[1] + abNorm[2] * bcNorm[2];
angle = arccos[res]*180.0/ pi
print angle
also the res can be calculated with dot:
res = abNorm[0] * bcNorm[0] + abNorm[1] * bcNorm[1] + abNorm[2] * bcNorm[2];
res = dot[abNorm, bcNorm]
answered Feb 3, 2016 at 12:18
adomas.madomas.m
3832 silver badges12 bronze badges
4
For 2D, you can use this method using the math library.
import math
def getAngle[a, b, c]:
ang = math.degrees[math.atan2[c[1]-b[1], c[0]-b[0]] - math.atan2[a[1]-b[1], a[0]-b[0]]]
return ang + 360 if ang < 0 else ang
print[getAngle[[5, 0], [0, 0], [0, 5]]]
Credits: //manivannan-ai.medium.com/find-the-angle-between-three-points-from-2d-using-python-348c513e2cd
answered Jan 25, 2021 at 18:23
0
In case you have a big list of [x,y,z] coordinates, this works:
import numpy
def compute_angle_between_3d_points[a,b,c]:
ba = a - b
bc = c - b
cosine_numerator = np.sum[ba*bc, axis=1]
cosine_denominator_1 = np.linalg.norm[ba, axis=1]
cosine_denominator_2 = np.linalg.norm[bc, axis=1]
cosine_angle = cosine_numerator / [cosine_denominator_1 * cosine_denominator_2]
angles = np.arccos[cosine_angle]
degree_angles = np.rad2deg[angles]
return degree_angles
Above, a,b,c are presumed to be of shape [N_Points, 3]. Something in TensorFlow or Torch would surely be faster, but there you go.
answered Aug 19 at 22:46
legellegel
2,2513 gold badges21 silver badges22 bronze badges