Python_level_2/2lession/Point.py

65 lines
1.4 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from math import sqrt
class PointXY:
def __init__(self, x: int, y: int):
self.x = x
self.y = y
def distance_from_zero(self):
return sqrt(pow(self.x, 2) + pow(self.y, 2))
def distance_to(self, p):
return sqrt(pow((self.x - p.x), 2) + pow((self.y - p.y), 2))
def __repr__(self):
return "Точка с координатами (" + str(self.x) + ", " + str(self.y) + ")"
def __eq__(self, point):
if self.distance_to(point) == 0:
result = True
else:
result = False
return result
def __ne__(self, point):
if self.distance_to(point) != 0:
result = True
else:
result = False
return result
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 main():
p1 = PointXY(4, 3)
p2 = PointXY(9, 6)
p3 = PointXY(5, 3)
print(p1.distance_from_zero())
print(p2.distance_to(p3))
print(p1 == p2)
print(p1 != p2)
p4 = p1 + p2
p5 = p1 * 0.5
p1 = -p2
p1 = ~p2
print(abs(p1))
if __name__ == '__main__':
main()