I am trying to calculate the angle between two lines in Python. I searched the Internet and found the equation on how to do it. But I don't always get an accurate result. Some of the results are clearly false when other seems correct.
My code is given below:
def angle(pt1, pt2):
m1 = (pt1.getY() - pt1.getY())/1
m2 = (pt2.getY() - pt1.getY())/(pt2.getX()-pt1.getX())
tnAngle = (m1-m2) / (1 + (m1*m2))
return math.atan(tnAngle)
def calculate(pt, ls):
i = 2
for x in ls:
pt2 = point(x, i)
i = i + 1
ang = angle(pt, pt2)*180/math.pi
ang = ang * (-1)
print ang
pt = point(3, 1)
ls = [1, 7, 0, 4, 9, 6, 150]
calculate(pt, ls)
The result it produces is:
45.0
0.0
45.0
-75.9637565321
0.0
-63.4349488229
0.0
The problem is that I don't understand why the second result, fifth and the last one are zeroed. They intersect since they share a point and the other point is not duplicated since the value in the array is different.