Python Forum
finding angle between three points on a 2d graph
Thread Rating:
  • 1 Vote(s) - 4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
finding angle between three points on a 2d graph
#1
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
Reply
#2
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.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
The function math.atan2() may help you solve this.
Reply
#4
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
Reply
#5
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)))
Output:
90.0
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 like
    if b in (a, c):
        raise ValueError("Undefined angle, two identical points", (a, b, c))
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  pylab, labeling points on a graph. Dasiey12 0 1,670 Apr-04-2021, 01:08 AM
Last Post: Dasiey12
  Adding graph points and formating project_science 4 2,347 Jan-24-2021, 05:02 PM
Last Post: project_science
  How to map 360 degree angle over 1024 counts breadcat248 3 2,440 May-17-2019, 07:13 AM
Last Post: breadcat248
  Converting Angle to X and Y Values: 90/180/270 deg qrani 1 2,737 Nov-21-2018, 06:41 PM
Last Post: woooee
  Angle kripso 12 29,110 Oct-30-2017, 11:33 PM
Last Post: kripso
  How to find the cosine of an angle sylas 6 5,143 May-25-2017, 04:29 PM
Last Post: sylas

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020