Skip to content

Instantly share code, notes, and snippets.

@garybradski
Created September 26, 2020 02:22
Show Gist options
  • Save garybradski/38edba951d43547e60356f841a26aa26 to your computer and use it in GitHub Desktop.
Save garybradski/38edba951d43547e60356f841a26aa26 to your computer and use it in GitHub Desktop.
Clockwise angle between points, Intersection between segments
def clockwide_angle_between(p1, p2):
'''
Compute clockwise angle between 2 [x,y] points
:param p1: First point
:type p1: np.array([x1,y1])
:param p2: Second point
:type p2: np.array([x2,y3])
:return: clockwise angle between pt1 and pt2
:rtype: float64
'''
ang1 = np.arctan2(*p1[::-1])
ang2 = np.arctan2(*p2[::-1])
return np.rad2deg((ang1 - ang2) % (2 * np.pi))
def ccw(A,B,C):
return (C[1]-A[1]) * (B[0]-A[0]) > (B[1]-A[1]) * (C[0]-A[0])
# Return true if line segments AB and CD intersect
# NOTE this doesn't handle edge cases of segment colinear or within another segment
def intersect(A,B,C,D):
'''
Compute whether line segments AB intersects CB
:param A,B,C,D: #These are [X,Y] pairs
:type A,B,C,D: # np.array([1,2])
:return: True if the line segment intersects, else False
:rtype: bool
'''
return ccw(A,C,D) != ccw(B,C,D) and ccw(A,B,C) != ccw(A,B,D)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment