-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScalarVectorClass.py
More file actions
33 lines (26 loc) · 1.06 KB
/
Copy pathScalarVectorClass.py
File metadata and controls
33 lines (26 loc) · 1.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
from math import sqrt
class Point(object):
def __init__(self, x, y):
self.x = x
self.y = y
def __add__(self,other):
if isinstance(other, Point):
return Point(self.x + other.x, self.y + other.y )
else:
return TypeError ("Expected Point but got %s" %type(other))
def __sub__(self,other):
if isinstance(other, Point):
return Point(self.x - other.x, self.y - other.y )
else:
return TypeError ("Expected Point but got %s" %type(other))
def __mul__(self,other):
if isinstance(other, Point):
return (self.x * other.x + self.y * other.y )
elif isinstance (other, int):
return Point(self.x * other , self.y * other )
else:
return TypeError ("Expected Point or Int but got %s" %type(other))
def distance(self, other):
return sqrt((self.x - other.x) ** 2 + (self.y - other.y) ** 2)
def __repr__(self):
return f"Point({self.x}, {self.y})"