def any_segments_intersect(S): '''This algorithm takes as input a set S of n line segments, returning the boolean value TRUE if any pair of segments in S intersects, and FALSE otherwise.''' T = rb_tree() segment_list = [] point_list = [] for s in S: segment_list.append(segment(s)) for s in segment_list: point_list.append(point([s[0][0], 0, s[0][1]], s)) point_list.append(point([s[1][0], 1, s[1][1]], s)) heap_point = max_heap(point_list) heap_point.heapsort() for p in heap_point: if p[1] == 0: s = p.segment T.insert(s) a = T.above(s) b = T.below(s) if (a != None and segments_intersect(a[0], a[1], s[0], s[1])) or (b != None and segments_intersect(b[0], b[1], s[0], s[1])): return True if p[1] == 1: s = p.segment a = T.above(s) b = T.below(s) # print( a) # print( b) # print( type(a)) # print( type(b)) if a != None and b != None and segments_intersect(a[0], a[1], b[0], b[1]): return True T.delete(s) return False
def any_segments_intersect(S): '''This algorithm takes as input a set S of n line segments, returning the boolean value TRUE if any pair of segments in S intersects, and FALSE otherwise.''' T = rb_tree() segment_list = [] point_list = [] for s in S: segment_list.append(segment(s)) for s in segment_list: point_list.append(point([s[0][0], 0, s[0][1]], s)) point_list.append(point([s[1][0], 1, s[1][1]], s)) heap_point = max_heap(point_list) heap_point.heapsort() for p in heap_point: if p[1] == 0: s = p.segment T.insert(s) a = T.above(s) b = T.below(s) if (a != None and segments_intersect(a[0], a[1], s[0], s[1])) or (b != None and segments_intersect(b[0], b[1], s[0], s[1])): return True if p[1] == 1: s = p.segment a = T.above(s) b = T.below(s) # print a # print b # print type(a) # print type(b) if a != None and b != None and segments_intersect(a[0], a[1], b[0], b[1]): return True T.delete(s) return False
def right_horizontal_ray_intersect(p0, p1, p2): '''An algorithm to determine whether a given right horizontal ray from p0 intersects a line segment p1p2''' max_x = max(p1[0], p2[0]) if max_x < p0[0]: return False # When max(x1, x2) = x0, if intersecting, the intersection point must be po, then po must be on the line segment p1p2 elif max_x == p0[1]: if p1[0] == p2[0] and min(p1[1], p2[1]) <= p0[1] and max(p1[1], p2[1]) >= p0[1]: return True else: return equal(p0, p1) or equal(p0, p2) else: return segments_intersect(p1, p2, p0, (max_x, p0[1]))
def right_horizontal_ray_intersect(p0, p1, p2): '''An algorithm to determine whether a given right horizontal ray from p0 intersects a line segment p1p2''' max_x = max(p1[0], p2[0]) if max_x < p0[0]: return False # When max(x1, x2) = x0, if intersecting, the intersection point must be po, then po must be on the line segment p1p2 elif max_x == p0[1]: if p1[0] == p2[0] and min(p1[1], p2[1]) <= p0[1] and max( p1[1], p2[1]) >= p0[1]: return True else: return equal(p0, p1) or equal(p0, p2) else: return segments_intersect(p1, p2, p0, (max_x, p0[1]))