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 f_equation(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.f_equation())
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
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.f_equation())
else:
plt.text(point.x, point.y, "("+str(point.x)+","+str(point.y) +
") Point is not on the line " + self.f_equation())
if __name__ == '__main__':
p1 = Point(1, 1)
#p1.print("black", "A")
p2 = Point(2, 4)
#p2.print("black", "B")
l1 = Line(p1, p2)
l1.print()
p3 = Point(2, 2)
#p3.print("black", "C")
p4 = Point(8, 8)
#p4.print("black", "D")
l2 = Line(p3, p4)
l2.print()
p_rand = Point(random.randint(0, 9), random.randint(0, 9))
p_rand.print("brown")
l2.pointContainsion(p_rand)
l2.vectorTranslation([2, 3])
l2.print()
p5 = Point(0, 3)
p5.print("red", '(0,3)')
p5.rotate(90)
p5.print("blue", '(0,3) rotated by 90°')
p5.reflection_OY()
p5.print("green", '(0,3) reflection by axis Y')
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()