![]() |
finding angle between three points on a 2d graph - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: finding angle between three points on a 2d graph (/thread-14188.html) |
finding angle between three points on a 2d graph - qrani - Nov-19-2018 So, i've been trying to find this out for such a long time. So say I have three points A, B, C. Each with X and Y values. ax = 1 ay = 5 bx = 1 by = 2 cx = 5 cy = 1 which looks something like this on a graph: 5 | A . . . . 4 | . . . . . 3 | . . . . . 2 | B . . . . 1 | . . . . C 0 * - - - - - * 0 1 2 3 4 5 and i want to find the angle between A & C as if it were around circle B also, 0-360 RE: finding angle between three points on a 2d graph - buran - Nov-19-2018 what have you tried? We are glad to help, but we won't do your [home]work. Post your code in python tags, any errors in error tags, ask specific questions. RE: finding angle between three points on a 2d graph - Gribouillis - Nov-19-2018 The function math.atan2() may help you solve this.
RE: finding angle between three points on a 2d graph - qrani - Nov-20-2018 here is what i have so far: import math p1x = 0 p1y = 5 p2x = 0 p2y = 0 p3x = 5 p3y = 0 angle1 = (math.atan2(p3y - p1y, p3x - p1x) - math.atan2(p2y - p1y, p2x - p1x)) print(angle1)based on something i found online, but not sure if it is correct, also it just gives 0.785(irrational) and i want a 0-360 number. edit: i changed it to deg with math.degrees, but still doesn't work to my full intentions and gives me 45 deg when its 90 deg RE: finding angle between three points on a 2d graph - Gribouillis - Nov-20-2018 It is almost correct. The order of the points matters import math def angle3pt(a, b, c): """Counterclockwise angle in degrees by turning from a to c around b Returns a float between 0.0 and 360.0""" ang = math.degrees( math.atan2(c[1]-b[1], c[0]-b[0]) - math.atan2(a[1]-b[1], a[0]-b[0])) return ang + 360 if ang < 0 else ang print(angle3pt((5, 0), (0, 0), (0, 5))) This function will not throw an error if a==b or c==b because math.atan2(0,0) returns 0.0. It would probably be a good idea to add this in the function, something likeif b in (a, c): raise ValueError("Undefined angle, two identical points", (a, b, c)) |