def actual_intersection(segments, i, j): segment = segments[i] other_segment = segments[j] if is_same(dot_product(points_diff(*segment), points_diff(*other_segment)), 0.0): return False for index1, p1 in enumerate(segment): for index2, p2 in enumerate(other_segment): if is_same(p1, p2): segment1 = segments[i - 1][index1], segments[(i + 1) % len(segments)][index1] segment2 = segments[j - 1][index2], segments[(j + 1) % len(segments)][index2] if have_common_point(segment1, segment2): return False if not segments_are_intersecting(segment1, segment2): return False # == if index1 == 0: connected_segment1 = segments[i - 1] else: connected_segment1 = segments[(i + 1) % len(segments)] if index2 == 0: connected_segment2 = segments[j - 1] else: connected_segment2 = segments[(j + 1) % len(segments)] if is_same( cross_product(points_diff(*connected_segment1), points_diff(*other_segment)), 0.0 ) or is_same(cross_product(points_diff(*connected_segment2), points_diff(*segment)), 0.0): return False center = p1 if _is_in_angle(segment1[0], center, segment1[1], segment2[0]) == _is_in_angle( segment1[0], center, segment1[1], segment2[1] ): return False for index1, p1 in enumerate(segment): if is_on_segment(other_segment[0], other_segment[1], p1): full_segment = (segments[i - 1][index1], segments[(i + 1) % len(segments)][index1]) if have_common_point(other_segment, full_segment): return False if not segments_are_intersecting(full_segment, other_segment): return False for index2, p2 in enumerate(other_segment): if is_on_segment(segment[0], segment[1], p2): full_segment = (segments[j - 1][index2], segments[(j + 1) % len(segments)][index2]) if have_common_point(segment, full_segment): return False if not segments_are_intersecting(full_segment, segment): return False return True
def _is_in_angle(p1, center, p2, point_to_check): v1 = normalize(points_diff(center, p1)) v2 = normalize(points_diff(center, p2)) v = normalize(points_diff(center, point_to_check)) return cross_product(v1, v) <= PRECISION and cross_product(v2, v) >= PRECISION