FootyStattowannab3 - 7 months ago 39

Python Question

So I have X & Y Co-ordinates within a loop. I want to calculate the angle (in degrees) at any given of Point C (at 40,60 as an example, Which will constnatly change) from Point A and Point B (A + B Co-ordinates will never change at (45,100) & (55,100)). I have drawn an image as guidance, How would I calculate this pythonically so I can calculate it within the loop.

Answer

The other question has a lot of neat ways in other languages, but nothing in Python. So I'll give it a shot.

```
import math
bx = 55
by = 100
ax = 45
ay = 100
cx = 40
cy = 60
angle1 = math.atan2(bx-cx, by-cy)
angle2 = math.atan2(ax-cx, ay-cy)
degrees = math.degrees(angle1 - angle2)
if degrees < 0:
degrees = 0 - degrees
if degrees > 180:
degrees = 360 - degrees
print (str)(cx) + ', ' + (str)(cy) + ' makes an angle of ' + (str)(degrees) + ' with 55,100 and 45, 100.';
```

http://www.tutorialspoint.com/python/number_atan2.htm used as a reference.

Atan2 deals cleanly with when ax = cx or bx e.g. either AC or BC is a vertical line, which is always a worrisome case since you get potential divide by zero errors.