MCPcopy Create free account
hub / github.com/ActiveState/code / Vector

Class Vector

recipes/Python/502252_Physics/recipe-502252.py:23–82  ·  view source on GitHub ↗

Vector(x, y) -> Vector

Source from the content-addressed store, hash-verified

21################################################################################
22
23class Vector:
24
25 'Vector(x, y) -> Vector'
26
27 def __init__(self, x, y):
28 'Initialize the Vector object.'
29 self.x = float(x)
30 self.y = float(y)
31
32 def __repr__(self):
33 'Return the vector\'s representation.'
34 return 'Vector(%r, %r)' % (self.x, self.y)
35
36 def __add__(self, vector):
37 'Return the sum of vector addition.'
38 return Vector(self.x + vector.x, self.y + vector.y)
39
40 def __sub__(self, vector):
41 'Return the difference of vector subtraction.'
42 return Vector(self.x - vector.x, self.y - vector.y)
43
44 def __mul__(self, number):
45 'Return the product of vector multiplication.'
46 return Vector(self.x * number, self.y * number)
47
48 def __div__(self, number):
49 'Return the quotient of vector division.'
50 return Vector(self.x / number, self.y / number)
51
52 def __iadd__(self, vector):
53 'Execute addition in-place.'
54 self.x += vector.x
55 self.y += vector.y
56 return self
57
58 def __isub__(self, vector):
59 'Execute subtraction in-place.'
60 self.x -= vector.x
61 self.y -= vector.y
62 return self
63
64 def __imul__(self, number):
65 'Execute multiplication in-place.'
66 self.x *= number
67 self.y *= number
68 return self
69
70 def __idiv__(self, number):
71 'Execute division in-place.'
72 self.x /= number
73 self.y /= number
74 return self
75
76 def __abs__(self):
77 'Return the vector\'s magnitude.'
78 return _math.hypot(self.x, self.y)
79
80 def unit(self):

Callers 7

__add__Method · 0.70
__sub__Method · 0.70
__mul__Method · 0.70
__div__Method · 0.70
__init__Method · 0.70
moveMethod · 0.70
moveFunction · 0.50

Calls

no outgoing calls

Tested by

no test coverage detected