def __init__(self, point1: Point, point2: Point = None): point1 = Point.check_Point(point1) if type(point2) is None: self.start_point: Point = Point(0, 0) self.end_point: Point = point1 else: point2 = Point.check_Point(point2) self.start_point: Point = point1 self.end_point: Point = point2
def line_intersection_point(vector1: FlatVector, vector2: FlatVector): # составляем формулы двух прямых x1_1 = vector1.start_point.x y1_1 = vector1.start_point.y x1_2 = vector1.end_point.x y1_2 = vector1.end_point.y x2_1 = vector2.start_point.x y2_1 = vector2.start_point.y x2_2 = vector2.end_point.x y2_2 = vector2.end_point.y A1 = y1_1 - y1_2 B1 = x1_2 - x1_1 C1 = x1_1 * y1_2 - x1_2 * y1_1 A2 = y2_1 - y2_2 B2 = x2_2 - x2_1 C2 = x2_1 * y2_2 - x2_2 * y2_1 # решаем систему двух уравнений if B1 * A2 - B2 * A1 != 0: y = (C2 * A1 - C1 * A2) / (B1 * A2 - B2 * A1) if B1 * A2 - B2 * A1 == 0: return False if A1 == 0: x = 0 else: x = (-C1 - B1 * y) / A1 # проверяем, находится ли решение системы (точка пересечения) на первом отрезке, min/max - потому # что координаты точки могут быть заданы не по порядку возрастания if min(x1_1, x1_2) <= x <= max(x1_1, x1_2) and min( y1_1, y1_2) <= y <= max(y1_1, y1_2): return Point(x, y) else: return False
def __sub__(self, other): if type(other) == FlatVector: return FlatVector(self.start_point - other.start_point, self.end_point - other.end_point) other = Point.check_Point(other) start = self.start_point - other end = self.end_point - other return FlatVector(start, end)
def __add__(self, other: Point or tuple or list): if type(other) == FlatVector: return FlatVector(self.start_point + other.start_point, self.end_point + other.end_point) other = Point.check_Point(other) start = self.start_point + other end = self.end_point + other return FlatVector(start, end)
def __mul__(self, other): other = Point.check_Point(other) start = self.start_point * other end = self.end_point * other return FlatVector(start, end)
def move_to_point(self, new_start_point: Point or tuple): new_start_point = Point.check_Point(new_start_point) dif_val = self.start_point - new_start_point self.start_point -= dif_val self.end_point -= dif_val