Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Python – Calculate angle from object1(as x and y) to object2(as x and y)

enter image description here
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 :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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).

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading