Created
March 9, 2018 18:29
-
-
Save Nate-Wessel/e0a549d75336659aafc3b33e53297570 to your computer and use it in GitHub Desktop.
Python function for calculating a planar inner angle of three points
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def inner_angle_plane(point1,point2,point3): | |
"""Given three points, p1 | |
calculate the smaller of the two angles \ a | |
formed by the sequence. \ | |
Returns degrees. p2------p3 | |
Input is three (x,y) tuples | |
""" | |
# be sure there IS an angle to measure | |
assert point1 != point2 and point2 != point3 | |
# get coordinate values | |
p1x,p1y = point1 | |
p2x,p2y = point2 | |
p3x,p3y = point3 | |
# the rounding is for rare precision issues with acos() at ~ 180 degrees | |
radians = math.acos( round( | |
( | |
( (p2x-p1x)**2 + (p2y-p1y)**2 ) + | |
( (p2x-p3x)**2 + (p2y-p3y)**2 ) - | |
( (p3x-p1x)**2 + (p3y-p1y)**2 ) | |
) / | |
math.sqrt( | |
4 * | |
( (p2x-p1x)**2 + (p2y-p1y)**2 ) * | |
( (p2x-p3x)**2 + (p2y-p3y)**2 ) | |
) | |
, 10 ) ) | |
degrees = math.degrees(radians) | |
assert degrees <= 180 | |
return degrees |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Well, my image works with 3-space tabs but not here :-/