I have a list of items that represents X,Y on a graph (all starts at point (0,0). example:
1. [(0,0),(0,1),(0,2),(1,2),(2,2)]
2. [(0,0),(0,1),(0,2),(1,2),(2,2),(2,1),(1,1),(0,1)]
item 2 is invalid because it intersect at point (0,1).
in order to find if intersection exists, I sort (nlogn) the list and iterate to find if 2 points are the same.
def is_intersect(points ):
# points [(0,0)...]
points.sort()
for m,u in zip(points,points[1:]):
if m==u:
return True
return False
My question: is there a better way to find an intersection than the above algorithm (with space complexity O(1) no extra set or hashset)?