def check(case_points, case_polygons): figure1_points = case_points[0] figure2_points = case_points[1] figure1 = case_polygons[0] figure2 = case_polygons[1] sum_points, product_points = is_outside(figure2_points, figure2, figure1_points) sum, product = is_outside(figure1_points, figure1, figure2_points) sum_points += sum product_points += product all_lines = figure1 + figure2 figure_intersections = find_figures_intersections(figure1, figure2, figure1_points, figure2_points, all_lines) sum_points += figure_intersections product_points += figure_intersections if len(product_points) == 0: print("Figures are separated") return sum_points_collection = PointsCollection(sum_points, color="blue") product_points_collection = PointsCollection(product_points, color="red") product_lines = get_result_lines(all_lines, product_points) product_lines_collection = LinesCollection(product_lines) sum_lines = get_result_lines(all_lines, sum_points) for line in product_lines: if line in sum_lines: sum_lines.remove(line) sum_lines_collection = LinesCollection(sum_lines, color = "green") scene1 = Scene([sum_points_collection], [sum_lines_collection]) scene2 = Scene([product_points_collection], [product_lines_collection]) plot = Plot([scene1]) plot.add_scene(scene2) plot.draw()
def initialize_polygons(case): plot = Plot([]) if case == 1: # 1 separable A = (1, 3) B = (4, 1) C = (5, 4) D = (7, 6) E = (3, 6) l1 = [A, B] l2 = [B, C] l3 = [C, D] l4 = [D, E] l5 = [E, A] points_list1_a = [A, B, C, D, E] polygon1_a = [l1, l2, l3, l3, l4, l5] points1 = PointsCollection(points_list1_a, color="blue") lines1 = LinesCollection(polygon1_a, color='green') a = (5, 3) b = (7, 1) c = (11, 4) d = (10, 7) e = (8, 6) l1 = [a, b] l2 = [b, c] l3 = [c, d] l4 = [d, e] l5 = [e, a] points_list1_b = [a, b, c, d, e] polygon1_b = [l1, l2, l3, l3, l4, l5] points2 = PointsCollection(points_list1_b, color="orange") lines2 = LinesCollection(polygon1_b, color='red') scene = Scene([points1, points2], [lines1, lines2]) plot.add_scene(scene) case_points = (points_list1_a, points_list1_b) case_polygon = (polygon1_a, polygon1_b) elif case == 2: # 2 common edge outside A = (1, 4) B = (3, 3) C = (4, 1) D = (6, 5) E = (4, 6) l1 = [A, B] l2 = [B, C] l3 = [C, D] l4 = [D, E] l5 = [E, A] points_list2_a = [A, B, C, D, E] polygon2_a = [l1, l2, l3, l3, l4, l5] points1 = PointsCollection(points_list2_a, color="blue") lines1 = LinesCollection(polygon2_a, color='green') a = (4, 1) b = (9, 1) c = (8, 3) d = (9, 6) e = (6, 5) l1 = [a, b] l2 = [b, c] l3 = [c, d] l4 = [d, e] l5 = [e, a] points_list2_b = [a, b, c, d, e] polygon2_b = [l1, l2, l3, l3, l4, l5] points2 = PointsCollection(points_list2_b, color="orange") lines2 = LinesCollection(polygon2_b, color='red') scene = Scene([points1, points2], [lines1, lines2]) plot.add_scene(scene) case_points = (points_list2_a, points_list2_b) case_polygon = (polygon2_a, polygon2_b) elif case == 3: # common top A = (1, 2) B = (4, 1) C = (5, 4) D = (3, 6) E = (2, 5) l1 = [A, B] l2 = [B, C] l3 = [C, D] l4 = [D, E] l5 = [E, A] points_list4_a = [A, B, C, D, E] polygon4_a = [l1, l2, l3, l3, l4, l5] points1 = PointsCollection(points_list4_a, color="blue") lines1 = LinesCollection(polygon4_a, color='green') a = (6, 1) b = (8, 3) c = (9, 6) d = (6, 5) e = (5, 4) l1 = [a, b] l2 = [b, c] l3 = [c, d] l4 = [d, e] l5 = [e, a] points_list4_b = [a, b, c, d, e] polygon4_b = [l1, l2, l3, l3, l4, l5] points2 = PointsCollection(points_list4_b, color="orange") lines2 = LinesCollection(polygon4_b, color='red') scene = Scene([points1, points2], [lines1, lines2]) plot.add_scene(scene) case_points = (points_list4_a, points_list4_b) case_polygon = (polygon4_a, polygon4_b) elif case == 4: # crossing A = (1, 2) B = (6, 1) C = (5, 5) D = (2, 6) E = (3, 4) l1 = [A, B] l2 = [B, C] l3 = [C, D] l4 = [D, E] l5 = [E, A] points_list5_a = [A, B, C, D, E] polygon5_a = [l1, l2, l3, l3, l4, l5] points1 = PointsCollection(points_list5_a, color="blue") lines1 = LinesCollection(polygon5_a, color='green') a = (3, 2) b = (7, 3) c = (10, 2) d = (9, 5) e = (4, 4) l1 = [a, b] l2 = [b, c] l3 = [c, d] l4 = [d, e] l5 = [e, a] points_list5_b = [a, b, c, d, e] polygon5_b = [l1, l2, l3, l3, l4, l5] points2 = PointsCollection(points_list5_b, color="orange") lines2 = LinesCollection(polygon5_b, color='red') scene = Scene([points1, points2], [lines1, lines2]) plot.add_scene(scene) case_points = (points_list5_a, points_list5_b) case_polygon = (polygon5_a, polygon5_b) else: # inside A = (1, 1) B = (5, 2) C = (10, 1) D = (8, 6) E = (2, 5) l1 = [A, B] l2 = [B, C] l3 = [C, D] l4 = [D, E] l5 = [E, A] points_list6_a = [A, B, C, D, E] polygon6_a = [l1, l2, l3, l3, l4, l5] points1 = PointsCollection(points_list6_a, color="blue") lines1 = LinesCollection(polygon6_a, color='green') a = (2, 3) b = (5, 3) c = (8, 2) d = (7, 4) e = (5, 5) l1 = [a, b] l2 = [b, c] l3 = [c, d] l4 = [d, e] l5 = [e, a] points_list6_b = [a, b, c, d, e] polygon6_b = [l1, l2, l3, l3, l4, l5] points2 = PointsCollection(points_list6_b, color="orange") lines2 = LinesCollection(polygon6_b, color='red') scene = Scene([points1, points2], [lines1, lines2]) plot.add_scene(scene) case_points = (points_list6_a, points_list6_b) case_polygon = (polygon6_a, polygon6_b) plot.draw() return case_points, case_polygon
def check_if_intersects(lines_list, max_y): lines_dictionary, q = read_lines_list(lines_list) start_points_used_dictionary = dict() end_points_used_dictionary = dict() for line in lines_dictionary: start_points_used_dictionary[line] = False end_points_used_dictionary[line] = False lines_collection = LinesCollection(lines_list) my_scene = Scene([], [lines_collection]) my_plot = Plot([my_scene]) #print("\n\nLines:") #for line in lines_list: # print(line) t = LinkedList(lines_dictionary) intersections_dictionary = dict() prev_point = None prev_line = None # Q: x->y while q.size > 0: event = q.findTheSmallest(q.root) point = (event.key, event.value) #print("\n-------------------------------------------------\n" + str(point), end="") # update T begin_line = is_start_point_checked(start_points_used_dictionary, point) end_line = [] if begin_line is None: end_line = is_end_point_checked(end_points_used_dictionary, point) if begin_line is not None: handle_line_begin_event(begin_line, t) #print("found line: " + str(begin_line)) elif end_line is not None: handle_line_end_event(end_line, t) #print("found line: " + str(end_line)) else: # intersection point handle_intersection_event(lines_dictionary, point, t, intersections_dictionary) # handle two line ends in the same point if prev_point == point: if end_line: #print("Two ends intersection in: " + str(point)) intersections_dictionary[point] = (prev_line, end_line) # update Q look_for_intersections(lines_dictionary, point, t, q, intersections_dictionary) q.delete(event.key, event.value) prev_line = end_line prev_point = point #print("\nintersections number: " + str(len(intersections_dictionary))) for intersection in intersections_dictionary: point = intersection line1 = intersections_dictionary[intersection][0] line2 = intersections_dictionary[intersection][1] #print(str(point) + " is intersection of " + str(line1) + " and " + str(line2)) new_line = [(point[0], max_y), (point[0], 1)] new_lines_collection = LinesCollection([new_line], color="red") points_collection = PointsCollection([point], color="green", marker="^") new_scene = Scene([points_collection], [lines_collection, new_lines_collection]) my_plot.add_scene(new_scene) my_plot.draw() return intersections_dictionary