def main(): # Координаты точек вершин первого многоугольника P = [Point(-5.0, 0.6), Point(-2, 2.1), Point(-7, 3.6), Point(-8, 2.1)] # P = [Point(5, -1), Point(8, 1), Point(8, 4), Point(6, 6), Point(3, 6), Point(1, 3), Point(2, 1)] # Координаты точек вершин второго многоугольника Q = [ Point(1, 2), Point(3, 0), Point(8, 0), Point(10, 2), Point(8, 4), Point(3, 4) ] # Q = [Point(16, 2), Point(21, 4), Point(20, 6), Point(16, 9), Point(13, 7), Point(13, 4)] reverse_polygon(P) reverse_polygon(Q) # Задаем направление точкам speed = 0.25 for point in P: point.set_direction([Vector2d(speed * 1, 0), speed]) for point in Q: point.set_direction([Vector2d(speed * -1, 0), speed]) plot_task(P, Q)
def reflect(p, vector_coords): # Previous direction v = p.direction # Polygon side q = Vector2d(vector_coords[0], vector_coords[1]) scal = 2 * (Vector2d.scalar_product(v, q) / Vector2d.scalar_product(q, q)) prod = Vector2d.s_mult(q, scal) new_direction = Vector2d.s_minus(prod, v) return new_direction
def check_triangle(p1, p2, p3): if p1 == p2 == p3: return p1 elif not p1 == p2 and not p1 == p3 and not p2 == p3: pos = check_point_pos(p1, p2, p3) if pos > 0: return [p1, p2, p3] elif pos < 0: return [p1, p3, p2] elif pos == 0: if Vector2d.scalar_product(Vector2d(p3, p1), Vector2d(p3, p2)) < 0: return [p1, p2] elif Vector2d.scalar_product(Vector2d(p1, p2), Vector2d(p1, p3)) < 0: return [p2, p3] elif Vector2d.scalar_product(Vector2d(p2, p1), Vector2d(p2, p3)) < 0: return [p1, p3] else: if p1 == p2: return [p1, p3] elif p1 == p3: return [p1, p2] elif p2 == p3: return [p1, p2]
def check_triangle(p1, p2, p3): if p1 == p2 == p3: return p1 elif not p1 == p2 and not p1 == p3 and not p2 == p3: pos = define_orientation(p1, p2, p3) if pos == "left": return [p1, p2, p3] elif pos == "right": return [p1, p3, p2] elif pos == 0: if Vector2d.scalar_product(Vector2d(p3, p1), Vector2d(p3, p2)) < 0: return [p1, p2] elif Vector2d.scalar_product(Vector2d(p1, p2), Vector2d(p1, p3)) < 0: return [p2, p3] elif Vector2d.scalar_product(Vector2d(p2, p1), Vector2d(p2, p3)) < 0: return [p1, p3] else: if p1 == p2: return [p1, p3] elif p1 == p3: return [p1, p2] elif p2 == p3: return [p1, p2]
def get_intersection(p1, p2, p3, p4): # p = p3 + t(p4 - p3) n = Vector2d(-(p2.y - p1.y), p2.x - p1.x) t = (Vector2d.scalar_product(n, Vector2d( p3, p1))) / (Vector2d.scalar_product(n, Vector2d(p3, p4))) p = Vector2d(p3, p4) return Point(p3.x + t * p.x, p3.y + t * p.y)
def cyrus_beck(segment, points): A = segment[0] B = segment[1] # T = {t0 = 0, t1 = 1} tA = 0 tB = 1 n = len(points) for i in range(len(points)): side = Vector2d(points[i], points[next_el(i, n)]) normal = Vector2d(side.y, -side.x) # A(1-t) + Bt = P t2 = B.y - A.y t1 = get_intersection(A, B, points[i], points[next_el(i, n)]).y - A.y t = t1 / t2 scalar_product = Vector2d.scalar_product(Vector2d(A, B), normal) # если ПВ if scalar_product < 0: tA = max(tA, t) else: # если ПП tB = min(tB, t) if tA > tB: return 0, 0 return tA, tB
plt.ioff() plt.show() if __name__ == '__main__': points_set = [ Point(1, 1), Point(4, 3), Point(2, 2), Point(4, 5), Point(9, 3), Point(6, 4), Point(3, 0), Point(8, 1), Point(2, 4), Point(1.5, 3), Point(7, 3), Point(10, 7), Point(4, 5) ] # Set points direction for point in points_set: point.set_direction(Vector2d.get_vector(random.uniform(0, 2 * pi), 0.1)) plot_task(points_set) plt.show()
if __name__ == '__main__': pylab.xlim(-1, 24) pylab.ylim(-2, 10) first_polygon = [ Point(5, -1), Point(2, 1), Point(1, 3), Point(3, 6), Point(6, 6), Point(8, 4), Point(8, 1) ] # Set Direction speed = 0.25 for point in first_polygon: point.set_direction([Vector2d(speed * 1, 0), speed]) second_polygon = [ Point(16, 2), Point(13, 4), Point(13, 7), Point(16, 9), Point(20, 6), Point(21, 4) ] for point in second_polygon: point.set_direction([Vector2d(speed * -1, 0), speed]) plot_task(first_polygon, second_polygon)
rectangle = [Point(1, 1), Point(1, 10), Point(15, 10), Point(15, 1)] # Our points points_set = [ Point(2, 2), Point(2, 3), Point(4, 2), Point(4, 5), Point(4, 8), Point(6, 2), Point(6, 9), Point(8, 5), Point(10, 3), Point(11, 9), Point(12, 5), Point(12, 8) ] points_set.sort(key=lambda point: (point.x, point.y)) points_set_y = points_set.copy() points_set_y.sort(key=lambda point: (point.y, point.x)) # min_dist = divide_and_rule(points_set) # print(min_dist) # Set points direction for point in points_set: point.set_direction( Vector2d.get_vector(random.uniform(0, 2 * pi), 0.09)) plot_task(rectangle, points_set, points_set_y)
def delaunay_triangulation(points): gr = graham_method(points) P = gr[0] CH = gr[1] l_s = line_segments(CH) # E = 3V - out_V - 3 edge_limit = 3 * len(points) - len(CH) - 3 triangulation = [] triangulation += l_s new_edge = [] for side in l_s: base = side # p_m = min(P, key=lambda p: cos_angle(vector(p, base[0]), vector(p, base[1]))) p_m = min(P, key=lambda p: cos_angle(Vector2d(p, base[0]), Vector2d(p, base[1]))) if [p_m, base[0]] in triangulation or [base[0], p_m] in triangulation: pass else: triangulation.append([p_m, base[0]]) new_edge.append([p_m, base[0]]) if [base[1], p_m] in triangulation or [p_m, base[1]] in triangulation: pass else: triangulation.append([base[1], p_m]) new_edge.append([base[1], p_m]) if len(triangulation) == edge_limit: return triangulation new_edge_loop = [] for i in new_edge: new_edge_loop.append(i) while True: for side in new_edge: base = side p_m = min(P, key=lambda p: cos_angle(Vector2d(p, base[0]), Vector2d(p, base[1]))) if [p_m, base[0]] in triangulation or [base[0], p_m ] in triangulation: pass else: triangulation.append([p_m, base[0]]) new_edge.append([p_m, base[0]]) if [base[1], p_m] in triangulation or [p_m, base[1] ] in triangulation: pass else: triangulation.append([base[1], p_m]) new_edge.append([base[1], p_m]) if len(triangulation) == edge_limit: return triangulation
def is_intersect(p1, p2, p3, p4): d1 = determinant(p3, p4, p3, p1) d2 = determinant(p3, p4, p3, p2) d3 = determinant(p1, p2, p1, p3) d4 = determinant(p1, p2, p1, p4) if d1 == d2 == d3 == d4 == 0: c1 = Vector2d.scalar_product(Vector2d(p1, p3), Vector2d(p1, p4)) c2 = Vector2d.scalar_product(Vector2d(p2, p3), Vector2d(p2, p4)) c3 = Vector2d.scalar_product(Vector2d(p3, p1), Vector2d(p3, p2)) c4 = Vector2d.scalar_product(Vector2d(p4, p1), Vector2d(p4, p2)) if c1 < 0 or c2 < 0 or c3 < 0 or c4 < 0: return True else: return False elif d1 * d2 <= 0 and d3 * d4 <= 0: return True else: return False