def is_inside(original_points, given_point): points = copy.deepcopy(original_points) points += [points[0]] if is_on(points, given_point): return "on" outside_point = find_min_max(points)[0] # Point(-1,-1) # outside_point = Point(-1, -1) outside_point.x -= 1 outside_point.y -= 1 if dimensional_test(points, given_point): while is_intersect_in_nodes(points, given_point, outside_point): # print("outside point moved") outside_point.y -= 0.1 counter = 0 for i in range(0, len(points) - 1): try: counter += is_intersect(given_point, outside_point, points[i], points[i + 1]) except: counter += is_intersect(given_point, outside_point, points[i], points[0]) if counter % 2 == 0: return "outside" else: return "inside" else: return "outside"
def find_visible_points(convex_hall, point_to_append): #найти угол между вектором нормали и вектором проекции. Если он тупой то ребро видимо sides = [] for point in convex_hall: counter = 0 for i in range(len(convex_hall)): if is_intersect(point_to_append, point, convex_hall[i], convex_hall[(i + 1) % len(convex_hall)]): counter += 1 if counter == 2: sides.append(point) else: break stop_point = point for point in reversed(convex_hall): if point == stop_point: break counter = 0 for i in range(len(convex_hall)): if is_intersect(point_to_append, point, convex_hall[i], convex_hall[(i + 1) % len(convex_hall)]): counter += 1 if counter == 2: sides.append(point) else: break for i in range(len(sides)): orientation_set = set() for j in range(len(sides) - 1): orientation_set.add( define_orientation(point_to_append, sides[(j + i) % len(sides)], sides[(j + i + 1) % len(sides)])) if len(orientation_set) == 1: return sides[i:] + sides[:i]
def collision(given_point, points, position): # position can be inside or outside next_position_point = Point(given_point.x + given_point.speed[0], given_point.y + given_point.speed[1]) flag = is_inside(points, next_position_point) if flag == position or flag == "on": for ind in range(len(points) - 1): if (is_intersect(points[ind], points[ind + 1], next_position_point, given_point)): b = Vector(points[ind], points[ind + 1]) if (is_intersect(points[-1], points[0], next_position_point, given_point)): b = Vector(points[-1], points[0]) a = Vector(given_point, next_position_point) try: arr = [] for point in points: arr.append(distance(point, given_point)) new_a = b * ((a * b) / (b * b) * 2) - a given_point.set_speed((new_a.vx, new_a.vy)) except: given_point.x, given_point.y = (48, 54)
def collision(given_point, points, position): # position can be inside or outside next_position_point = Point(given_point.x + given_point.speed[0], given_point.y + given_point.speed[1]) flag = is_inside(points, next_position_point) if flag == position or flag == "on": for ind in range(len(points) - 1): if (is_intersect(points[ind], points[ind + 1], next_position_point, given_point)): b = Vector(points[ind], points[ind + 1]) if (is_intersect(points[-1], points[0], next_position_point, given_point)): b = Vector(points[-1], points[0]) a = Vector(given_point, next_position_point) try: arr = [] for point in points: arr.append(distance(point, given_point)) new_a = b * ((a * b) / (b * b) * 2) - a given_point.set_speed((new_a.vx, new_a.vy)) except: given_point.x, given_point.y = ( 48, 54 ) #тут иногда возникает исключение, когда точка попадает прямо в угол. тогда формула неправильная
def is_simple(original_points): points = deepcopy(original_points) points = points + [points[0]] # print(points) for i in range(0, len(points)): total = 0 for j in range(0, len(points)): try: total += is_intersect(points[i], points[i + 1], points[j], points[j + 1]) except: pass if total > 3: return False if total != 3 and total != 0: return False return True
def polygon_intersection(polygon1, polygon2): p_index, q_index = get_lines(polygon1, polygon2) res = [] while True: pq = is_aimed(polygon1[p_index], polygon1[(p_index + 1) % len(polygon1)], polygon2[q_index], polygon2[(q_index + 1) % len(polygon2)]) qp = is_aimed(polygon2[q_index], polygon2[(q_index + 1) % len(polygon2)], polygon1[p_index], polygon1[(p_index + 1) % len(polygon1)]) # p is aimed on q and q is aimed on p if pq and qp: if is_point_outer(polygon1[p_index], polygon1[(p_index + 1) % len(polygon1)], polygon2[(q_index + 1) % len(polygon2)]): q_index += 1 if q_index + 1 < len( polygon2) else -len(polygon2) + 1 else: p_index += 1 if p_index + 1 < len( polygon1) else -len(polygon1) + 1 # p is aimed on q not q is not aimed p elif pq and not qp: if not is_point_outer(polygon2[q_index], polygon2[(q_index + 1) % len(polygon2)], polygon1[(p_index + 1) % len(polygon1)]): res.append(polygon1[(p_index + 1) % len(polygon1)]) p_index += 1 if p_index + 1 < len(polygon1) else -len(polygon1) + 1 # p is not aimed q not q is aimed on p elif not pq and qp: if not is_point_outer(polygon1[p_index], polygon1[(p_index + 1) % len(polygon1)], polygon2[(q_index + 1) % len(polygon2)]): res.append(polygon2[(q_index + 1) % len(polygon2)]) q_index += 1 if q_index + 1 < len(polygon2) else -len(polygon2) + 1 # p is not aimed on q not q is not aimed on p else: if is_intersect(polygon1[p_index], polygon1[(p_index + 1) % len(polygon1)], polygon2[q_index], polygon2[(q_index + 1) % len(polygon2)]): res.append( find_intersection_point( polygon1[p_index], polygon1[(p_index + 1) % len(polygon1)], polygon2[q_index], polygon2[(q_index + 1) % len(polygon2)])) if define_orientation( polygon1[p_index], polygon1[(p_index + 1) % len(polygon1)], polygon2[(q_index + 1) % len(polygon2)]) == 'right': q_index += 1 if q_index + 1 < len( polygon2) else -len(polygon2) + 1 else: p_index += 1 if p_index + 1 < len( polygon1) else -len(polygon1) + 1 if len(res) > 1: if res[0] == res[-1]: res.pop() break return res