GO_lab_03.py

GO_lab_03.py
import random
import math
import numpy as np
from matplotlib import pyplot as plt


class Point:
    def __init__(self, x: float, y: float):
        self.x = x
        self.y = y

    def rotate(self, angle: int):
        X = round(self.x*math.cos(angle*math.pi/180) -
                  self.y*math.sin(angle*math.pi/180), 2)
        Y = round(self.y*math.cos(angle*math.pi/180) -
                  self.x*math.sin(angle*math.pi/180), 2)
        self.x = X
        self.y = Y

    def reflection_OX(self):
        self.y *= -1    # reflection about the axis X

    def reflection_OY(self):
        self.x *= -1    # reflection about the axis Y

    def print(self, color: str, text: str | None = ''):
        plt.scatter(self.x, self.y, color=color)  # plotting single point
        plt.text(self.x, self.y, " "+text)


class Line:
    def __init__(self, head: Point, tail: Point):
        self.head = head
        self.tail = tail
        self.calculateFunction()

    def fEquation(self) -> str:
        if self.b > 0:
            return ("y = {}x + {}".format(self.a, self.b))
        elif self.b == 0:
            return ("y = {}x ".format(self.a))
        elif self.b < 0:
            return ("y = {}x - {}".format(self.a, self.b*(-1)))

    def print(self):
        x = np.arange(self.head.x, self.tail.x+1)
        plt.plot(x, self.a*x+self.b)
        plt.text(self.tail.x, self.tail.y, " "+self.fEquation())

    def calculateFunction(self):
        self.a = (self.head.y - self.tail.y)/(self.head.x-self.tail.x)
        self.b = self.head.y - self.a * self.head.x
        self.A = self.a
        self.B = -1
        self.C = self.b

    def vectorTranslation(self, vector: list):
        self.head.x += vector[0]
        self.head.y += vector[1]
        self.tail.x += vector[0]
        self.tail.y += vector[1]
        self.calculateFunction()

    def pointContainsion(self, point: Point):
        if self.a * point.x + self.b - point.y == 0:
            if point.x >= self.head.x and point.x <= self.tail.x:
                plt.text(point.x, point.y, "Point belongs to the line segment")
            else:
                plt.text(point.x, point.y,
                         "Point belongs to the line" + self.fEquation())
        else:
            plt.text(point.x, point.y, "("+str(point.x)+","+str(point.y) +
                     ") Point is not on the line " + self.fEquation())

    def whichSide(self, point: Point) -> int:
        side = self.a*point.x-point.y+self.b
        if side < 0:
            print("Lewa")
            return -1
        elif side == 0:
            return 0
        elif side > 0:
            print("Prawa")
            return 1


def crossingPointCramer(l1: Line, l2: Line) -> Point:
    W = (l1.A*l2.B)-(l2.A*l1.B)
    Wx = ((-l1.C)*l2.B)-((-l2.C)*l1.B)
    Wy = (l1.A*(-l2.C))-(l2.A*(-l1.C))
    if W != 0:
        x = Wx/W
        y = Wy/W
        return Point(x, y)
    return Point(None, None)


def crossingPointCramerLineSegment(l1: Line, l2: Line) -> Point:
    W = (l1.A*l2.B)-(l2.A*l1.B)
    Wx = ((-l1.C)*l2.B)-((-l2.C)*l1.B)
    Wy = (l1.A*(-l2.C))-(l2.A*(-l1.C))
    if W != 0:
        x = Wx/W
        y = Wy/W
        if ((x >= l1.head.x and x <= l1.tail.x) or (x <= l1.head.x and x >= l1.tail.x)):
            return Point(x, y)
    return Point(None, None)


def triangleArea(P1: Point, P2: Point, P3: Point) -> int:
    a = (P2.x-P1.x, P2.y-P1.y)
    b = (P3.x-P1.x, P3.y-P1.y)

    P = 1/2*((a[0]*b[1])-(a[1]*b[0]))
    return P


if __name__ == '__main__':
    p1 = Point(4, 2)
    p2 = Point(8, 10)
    l1 = Line(p1, p2)
    l1.print()

    p3 = Point(2, 2)
    p4 = Point(8, 8)
    l2 = Line(p3, p4)
    l2.print()

    l1.whichSide(p3)

    p5 = crossingPointCramer(l1, l2)
    p5.print("yellow", "({},{})".format(p5.x, p5.y))

    t1 = Point(0, 0)
    t1.print("black", "A")
    t2 = Point(2, 1)
    t2.print("black", "B")
    t3 = Point(1, 1)
    t3.print("black", "C")

    print("Pole trójkąta t1, t2, t3: ", triangleArea(t1, t2, t3))

    plt.title("Jakub Litewka GO lab_02")
    plt.xlabel("x axis caption")
    plt.ylabel("y axis caption")
    plt.axhline(0, alpha=0.3)  # x-axis line
    plt.axvline(0, alpha=0.3)  # y-axis line
    plt.xlim(-4, 12)
    plt.ylim(-4, 12)
    plt.show()