Say you have two coordinates within a range of 0.0 till 1.0. A: (0.12, 0.75) B: (0.43, 0.97) And you want to walk from point A to point B. How would you calculate the angle in python.Where north is 0 and south is 180.
CodePudding user response:
if you want to get it from North then here you go:
def get_angle(x1,y1,x2,y2):
return math.degrees(math.atan2((x2-x1),(y2-y1)))
This works because
tan(x) = Opposite/Adjacent;
Opposite/Adjacent = x2-x1/y2-y1;
x=atan((x2-x1)/(y2-y1))
atan2 is the inverse of tan btw.
EDIT: use atan2
not atan
since atan2
returns -180 to 180 instead of atan
's -90 to 90.
CodePudding user response:
I would calculate the diference in x and the diference in y and use the function math.atan(y/x)
from the math
module. Then it's just a matter of converting from radians to degrees and add 90º.