7
7
from math import sqrt
8
8
from functools import total_ordering
9
9
10
- @total_ordering
10
+
11
+ @total_ordering
11
12
class Vector2D :
12
13
"""Vector2D class to perform simple vector operations.
13
14
"""
15
+
14
16
def __init__ (self , x : SupportsFloat = 0 , y : SupportsFloat = 0 ):
15
17
"""Creates a vector instance with the given x and y values.
16
-
18
+
17
19
Parameters
18
20
----------
19
21
x : number
20
22
x-Coordinate, by default 0
21
23
y : number
22
24
y-Coordinate, by default 0
23
-
25
+
24
26
Raises
25
27
------
26
28
TypeError
@@ -34,7 +36,7 @@ def __init__(self, x: SupportsFloat = 0, y: SupportsFloat = 0):
34
36
35
37
def __call__ (self ) -> str :
36
38
"""Callable for the vector instance to return its representation.
37
-
39
+
38
40
Returns
39
41
-------
40
42
str
@@ -45,7 +47,7 @@ def __call__(self) -> str:
45
47
46
48
def __repr__ (self ) -> str :
47
49
"""The vector instance representation.
48
-
50
+
49
51
Returns
50
52
-------
51
53
str
@@ -55,7 +57,7 @@ def __repr__(self) -> str:
55
57
56
58
def __str__ (self ) -> str :
57
59
"""The vector instance as a string.
58
-
60
+
59
61
Returns
60
62
-------
61
63
str
@@ -65,7 +67,7 @@ def __str__(self) -> str:
65
67
66
68
def __bool__ (self ) -> bool :
67
69
"""Returns the truth value of the vector instance.
68
-
70
+
69
71
Returns
70
72
-------
71
73
bool
@@ -76,7 +78,7 @@ def __bool__(self) -> bool:
76
78
77
79
def __abs__ (self ) -> float :
78
80
"""Returns the length (magnitude) of the vector instance
79
-
81
+
80
82
Returns
81
83
-------
82
84
float
@@ -86,28 +88,29 @@ def __abs__(self) -> float:
86
88
87
89
def check_vector_types (self , vector2 : Vector2D ):
88
90
"""Checks if the self and vector2 are an instance of the Vector2D class.
89
-
91
+
90
92
Parameters
91
93
----------
92
94
vector2 : Vector2D
93
95
Other vector (right of the operator).
94
-
96
+
95
97
Raises
96
98
------
97
99
TypeError
98
100
If self, or vector2 are not an instance of the Vector2D class.
99
101
"""
100
102
if not isinstance (self , Vector2D ) or not isinstance (vector2 , Vector2D ):
101
- raise TypeError ('You have to pass in two instances of the vector class!' )
103
+ raise TypeError (
104
+ 'You have to pass in two instances of the vector class!' )
102
105
103
106
def __eq__ (self , other_vector : Any ) -> bool :
104
107
"""Check if the vector instances have the same values.
105
-
108
+
106
109
Parameters
107
110
----------
108
111
other_vector : Vector2D
109
112
Other vector instance (right-hand-side of the operator)
110
-
113
+
111
114
Returns
112
115
-------
113
116
bool
@@ -122,12 +125,12 @@ def __eq__(self, other_vector: Any) -> bool:
122
125
123
126
def __lt__ (self , other_vector : Vector2D ) -> bool :
124
127
"""Check if the self instance is less than the other vector instance.
125
-
128
+
126
129
Parameters
127
130
----------
128
131
other_vector : Vector2D
129
132
Other vector instance (right-hand-side of the operator)
130
-
133
+
131
134
Returns
132
135
-------
133
136
bool
@@ -142,12 +145,12 @@ def __lt__(self, other_vector: Vector2D) -> bool:
142
145
143
146
def __add__ (self , other_vector : Vector2D ) -> Vector2D :
144
147
"""Returns the additon vector of the self and the other vector instance.
145
-
148
+
146
149
Parameters
147
150
----------
148
151
other_vector : Vector2D
149
152
Other vector instance (right-hand-side of the operator)
150
-
153
+
151
154
Returns
152
155
-------
153
156
Vector2D
@@ -160,12 +163,12 @@ def __add__(self, other_vector: Vector2D) -> Vector2D:
160
163
161
164
def __sub__ (self , other_vector : Vector2D ) -> Vector2D :
162
165
"""Returns the subtraction vector of the self and the other vector instance.
163
-
166
+
164
167
Parameters
165
168
----------
166
169
other_vector : Vector2D
167
170
Other vector instance (right-hand-side of the operator)
168
-
171
+
169
172
Returns
170
173
-------
171
174
Vector2D
@@ -178,12 +181,12 @@ def __sub__(self, other_vector: Vector2D) -> Vector2D:
178
181
179
182
def __mul__ (self , other : Union [Vector2D , SupportsFloat ]) -> Union [Vector2D , SupportsFloat ]:
180
183
"""Returns the multiplication of the self vector and the other vector(or number) instance.
181
-
184
+
182
185
Parameters
183
186
----------
184
187
other : Vector2D or number
185
188
Other vector instance or scaler value (right-hand-side of the operator)
186
-
189
+
187
190
Returns
188
191
-------
189
192
Vector2D
@@ -194,16 +197,17 @@ def __mul__(self, other: Union[Vector2D, SupportsFloat]) -> Union[Vector2D, Supp
194
197
elif isinstance (other , numbers .Real ):
195
198
return Vector2D (self .x * other , self .y * other )
196
199
else :
197
- raise TypeError ('You must pass in a vector instance or an int/float number!' )
200
+ raise TypeError (
201
+ 'You must pass in a vector instance or an int/float number!' )
198
202
199
203
def __truediv__ (self , other : SupportsFloat ) -> Vector2D :
200
204
"""Returns the multiplication of the self vector and the other vector(or number) instance.
201
-
205
+
202
206
Parameters
203
207
----------
204
208
other : Vector2D or number
205
209
Other vector instance or scaler value (right-hand-side of the operator)
206
-
210
+
207
211
Returns
208
212
-------
209
213
Vector2D
0 commit comments