98 lines
2.8 KiB
Python
98 lines
2.8 KiB
Python
from math import sqrt
|
|
from multipledispatch import dispatch
|
|
|
|
class PointXY():
|
|
def distance_from_zero(point):
|
|
return sqrt( point.x ** 2 + point.y ** 2 )
|
|
|
|
def distance_to(self, point):
|
|
return sqrt( (point.x - self.x) ** 2 + (point.y - self.y) ** 2)
|
|
|
|
def if_inside(self, r):
|
|
return (r.a.x <= self.x <= r.b.x) and (r.a.y <= self.y <= r.b.y)
|
|
|
|
def __str__(self):
|
|
return 'x = ' + str(self.x) + ' y = ' + str(self.y)
|
|
|
|
def __eq__(self, point):
|
|
return self.distance_to(point) == 0
|
|
|
|
def __ne__(self, point):
|
|
return self.distance_to(point) != 0
|
|
|
|
def __add__(self, point):
|
|
return PointXY(self.x + point.x, self.y + point.y)
|
|
|
|
def __mul__(self, multiplier):
|
|
return PointXY(self.x * multiplier, self.y * multiplier)
|
|
|
|
def __neg__(self):
|
|
return PointXY(-self.x, -self.y)
|
|
|
|
def __invert__(self):
|
|
return PointXY(-self.x, -self.y)
|
|
|
|
def __abs__(self):
|
|
return sqrt(pow(self.x, 2) + pow(self.y, 2))
|
|
|
|
def __init__(self, x:int, y:int):
|
|
self.x = x
|
|
self.y = y
|
|
|
|
class Rect():
|
|
def area(self):
|
|
return self.lenght * self.width
|
|
|
|
def perimeter(self):
|
|
return (self.lenght + self.width) * 2
|
|
|
|
def scale(self, coef):
|
|
self.lenght *= coef
|
|
self.width *= coef
|
|
|
|
def rotate(self):
|
|
self.lenght, self.width = self.width, self.lenght
|
|
|
|
def has_inside(self, p: PointXY):
|
|
if (self.a.x <= p.x <= self.b.x) and (self.a.y <= p.y <= self.b.y):
|
|
return 'точка внутри прямоуголника'
|
|
else:
|
|
return 'точка вне прямоугольника'
|
|
|
|
@dispatch(int, int)
|
|
def __init__(self, x: int, y: int):
|
|
self.lenght = x
|
|
self.width = y
|
|
|
|
@dispatch(PointXY, PointXY)
|
|
def __init__(self, a: PointXY, b: PointXY):
|
|
self.a = a
|
|
self.b = b
|
|
self.lenght = sqrt((a.x - a.x) ** 2 + (b.x - a.y) ** 2)
|
|
self.width = sqrt((b.x - a.x) ** 2 + (b.y - b.x) ** 2)
|
|
|
|
def __str__(self):
|
|
if str(hasattr(self, 'a')) == 'False':
|
|
return 'x = ' + str(self.lenght) + ' y = ' + str(self.width)
|
|
else:
|
|
return 'x = ' + str(self.lenght) + ' y = ' + str(self.width) + ' a(' + str(self.a.x) + ':' + str(self.a.y) + ')' + ' b(' + str(self.b.x) + ':' + str(self.b.y) + ')'
|
|
|
|
class Display(Rect):
|
|
def __init__(self, diagonal, height: int, width: int):
|
|
super().__init__(height, width)
|
|
self.diagonal = diagonal
|
|
self.dpi = round((sqrt(pow(self.height, 2) + pow(self.width, 2))) / self.diagonal, 2)
|
|
|
|
def get_dpi(self):
|
|
return self.dpi
|
|
|
|
def get_pixel_num(self):
|
|
return super().area()
|
|
|
|
def area(self):
|
|
return round((self.height / self.get_dpi()) * (self.width / self.get_dpi()), 1)
|
|
|
|
def get_resolution_px(self):
|
|
return self.height, self.width
|
|
|