Skip to content

Instantly share code, notes, and snippets.

@b27lu
Created February 2, 2014 02:52
Show Gist options
  • Save b27lu/8762337 to your computer and use it in GitHub Desktop.
Save b27lu/8762337 to your computer and use it in GitHub Desktop.
This gist shows how to determine if two line segments intersects.
#define a class to represent the x and y coordinates of points
class Point:
def __init__(self,x,y):
self.x = x
self.y = y
#judges if point A,B,C are counter-clockwise
def ccw(A,B,C):
return (C.y-A.y)*(B.x-A.x) > (B.y-A.y)*(C.x-A.x)
#determining if line segments AB and CD intersects
def intersect(A,B,C,D):
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