80 lines
2.2 KiB
Python
80 lines
2.2 KiB
Python
import math
|
|
import matplotlib.pyplot as plt
|
|
|
|
class Point:
|
|
__x__ = 0.0
|
|
__y__ = 0.0
|
|
|
|
def __init__(self, x = 0.0, y = 0.0):
|
|
self.__x__ = x
|
|
self.__y__ = y
|
|
|
|
def __str__(self):
|
|
return f"({self.__x__:.3f},{self.__y__:.3f})"
|
|
|
|
def __neg__(self):
|
|
return Point(-self.__x__, -self.__y__)
|
|
|
|
def __add__(self, other):
|
|
x = 0.0
|
|
y = 0.0
|
|
|
|
if isinstance(other, Point):
|
|
return Point(self.__x__ + other.__x__, self.__y__ + other.__y__)
|
|
elif isinstance(other, int) or isinstance(other, float):
|
|
return Point(self.__x__ + other, self.__y__ + other)
|
|
else:
|
|
raise ValueError("Operand must be Point, int or float.")
|
|
|
|
def __radd__(self, other):
|
|
return self + other
|
|
|
|
def __sub__(self, other):
|
|
if isinstance(other, Point) or isinstance(other, int) or isinstance(other, float):
|
|
return self + (-other)
|
|
else:
|
|
raise ValueError("Operand must be Point, int or float.")
|
|
|
|
def __rsub__(self, other):
|
|
return self - other
|
|
|
|
def __mul__(self, other):
|
|
if isinstance(other, Point):
|
|
return Point(self.__x__ * other.__x__, self.__x__ * other.__y__)
|
|
elif isinstance(other, int) or isinstance(other, float):
|
|
return Point(self.__x__ * other, self.__x__ * other)
|
|
else:
|
|
raise ValueError("Operand must be Point, int or float.")
|
|
|
|
def __rmul__(self, other):
|
|
return self * other
|
|
|
|
def rot(self, degree):
|
|
x = math.cos(degree) * self.__x__ + math.sin(degree) * self.__y__
|
|
y = -math.sin(degree) * self.__x__ + math.cos(degree) * self.__y__
|
|
return Point(x, y)
|
|
|
|
def get_x(self):
|
|
return self.__x__
|
|
|
|
def get_y(self):
|
|
return self.__y__
|
|
|
|
p1 = Point()
|
|
p2 = Point(1, 1)
|
|
p3 = Point (3, 2)
|
|
p4 = 2 * p2 + p3 - 1
|
|
p5 = p4.rot(math.pi / 2)
|
|
|
|
plt.plot(p1.get_x(), p1.get_y(), "orange", marker = "o")
|
|
plt.plot(p2.get_x(), p2.get_y(), "green", marker = "o")
|
|
plt.plot(p3.get_x(), p3.get_y(), "yellow", marker = "o")
|
|
plt.plot(p4.get_x(), p4.get_y(), "purple", marker = "o")
|
|
plt.plot(p5.get_x(), p5.get_y(), "purple", marker = "o")
|
|
|
|
plt.title("Points")
|
|
plt.xlabel("x")
|
|
plt.ylabel("y")
|
|
|
|
plt.show()
|