
I need exemple in Python, how to calc function like:
AngleFromObject1ToObject2InDegrees(object1, object2) # return float from 0.0 to 359.00 like 0-360 degrees, 360 = 0
>Solution :
This is a classic use case for the math.atan2 function:
from math import atan2, degrees
def AngleFromObject1ToObject2InDegrees(object1, object2):
return degrees(atan2(object2.y - object1.y, object2.x - object1.x))
Note that this uses the mathematical convention of angles that start at zero along the positive x-axis (to the right), and increase counter-clockwise. If you want 0 to be up and for them to increase clockwise (like the bearings on a magnetic compass’s dial), you can swap the arguments to atan2. You also might consider keeping your angle in radians, it’s a lot more convenient that way (since other trigonometric functions expect radians).