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
#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


Messages In This Thread
RE: finding angle between three points on a 2d graph - by Gribouillis - Nov-20-2018, 06:10 AM

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