-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvector.py
67 lines (45 loc) · 1.59 KB
/
vector.py
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
"""
0n this example code, I'm using dunder methods to compare two
different objects and know if one is greater than or less than.
Course: Python objects with special methods - Create better classes!.
Section: 3 Equality Operators.
Chapter: 12. Comparing and sorting.
- Rich comparison methods -
__eq__ -> Equality.
__ne__ -> Not Equal. This method is implicit.
__lt__ -> less than operator.
__le__ -> less or equal operator.
__gt__ -> greater than operator.
__ge__ -> greater or equal operator.
"""
import math
class Vector2:
def __init__(self, x1, y1, x2, y2):
self.x1 = x1
self.y1 = y1
self.x2 = x2
self.y2 = y2
def magnitude(self):
return math.sqrt((self.x2 - self.x1) ** 2 + (self.y2 - self.y1) ** 2)
def __lt__(self, other):
return self.magnitude() < other.magnitude()
def __le__(self, other):
return self.magnitude() <= other.magnitude()
#v1= Vector2(0,0,2,2) #magnitude 2.828
v1= Vector2(0,0,4,3)
v2 = Vector2(0,0,4,3) #magnitude 5
print(v1.magnitude())
print(v2.magnitude())
#compare the magnitudes
"""
The following comparison will rise an error:
print(v1 < v2)
TypeError: '<' not supported between instances of 'Vector2' and 'Vector2'
To avoid that error we can use __lt__ "less than operator" and use the comparator print(v1 < v2)
Also, we can use print(v1 > v2) without use __gt__ because it's a reflection of __lt__.
__le__ anable
"""
print(v1 < v2)
print(v1 > v2)
print(v1 >= v2)
print(v1 <= v2)