-
Notifications
You must be signed in to change notification settings - Fork 0
/
vector.py
195 lines (158 loc) · 5.53 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
class Vector(object):
"""
Custom Vector class
Could not find a Vector package with nescessary functionality
No clue why this doesn't exist yet...
"""
def __init__(self, *args, **kargs):
"""
args are the individual coordinate values ie x, y, z
Can have a vector of any dimension
"""
#Might should add a TypeError if there are non number args
if isinstance(args[0], (tuple, list)):
self.position = list(args[0])
else:
self.position = list(args)
def printVector(self):
"""
Print the vector
"""
print self.position
#Accessors/Mutators#
def getPoint(self, index):
"""
Gets the value in the position tuple of given index
Assumes the user will use 1 for the first coor
"""
return self.position[index-1]
def setPoint(self, index, value):
"""
Sets an index in position to the given value
Assumes the user will use 1 for the first coor
"""
self.position[index - 1] = value
#OVERRIDES#
def __eq__(self, other):
"""
override equals operator
"""
return self.position == other.position
def __abs__(self):
"""
override absolute value function
"""
return Vector(tuple([abs(coor) for coor in self.position]))
def __add__(self, other):
"""
implements the addition operator
"""
if len(self.position) != len(other.position):
raise ValueError ("The vectors you are trying to add are of different Dimensions")
else:
return Vector([self.position[i] + other.position[i] for i in range(len(self.position))])
def __sub__(self, other):
if len(self.position) != len(other.position):
raise ValueError ("The vectors you are trying to add are of different Dimensions")
else:
return Vector([self.position[i] - other.position[i] for i in range(len(self.position))])
def __mul__(self, other):
"""
multiple a vector with a scalar
"""
if isinstance(other, (int, long, float)):
raise ValueError ("Must multiply by int, long, or float. Use dot or cross methods for vector-vector multiplication")
else:
return Vector([coor * other for coor in self.position])
def __div__(self, other):
"""
divide vector by a scalar
"""
if not isinstance(other, (int, long, float)):
raise ValueError ("You must divide by an int, long, or float")
else:
return Vector([float(coor) / other for coor in self.position])
def __mod__(self, other):
pass
def __len__(self):
"""
overrides len function to return dimension of the vector
"""
return len(self.position)
class Vector2D(Vector):
def __init__(self, x, y):
"""
Use Super's constructor and add x, y variables for easy access
"""
Vector.__init__(self, x, y)
#works with python magic
self.x = x
self.y = y
#Accessors/Mutators#
def __getattribute__(self, name):
"""
Some python magic to pull the x and y values from position
"""
if name == 'x':
return object.__getattribute__(self, 'position')[0]
elif name == 'y':
return object.__getattribute__(self, 'position')[1]
else:
return object.__getattribute__(self, name)
def __setattr__(self, name, value):
"""
More python magic to have attribute setting to position
"""
if name == 'x':
old = self.position
old[0] = value
object.__setattr__(self, 'x', None)
object.__setattr__(self, 'position', old)
elif name == 'y':
old = self.position
old[1] = value
object.__setattr__(self, 'y', None)
object.__setattr__(self, 'position', old)
else:
object.__setattr__(self, name, value)
class Vector3D(Vector):
def __init__(self, x, y, z):
Vector.__init__(self, x, y, z)
#works with python magic methods
self.x = x
self.y = y
self.z = z
#Accessors and Mutators#
def __getattribute__(self, name):
"""
Some python magic to pull the x, y, and z values from position
"""
if name == 'x':
return object.__getattribute__(self, 'position')[0]
elif name == 'y':
return object.__getattribute__(self, 'position')[1]
elif name == 'z':
return object.__getattribute__(self, 'position')[2]
else:
return object.__getattribute__(self, name)
def __setattr__(self, name, value):
"""
More Python magic to set x, y, z from IE Vector3D.x to position
"""
if name == 'x':
old = self.position
old[0] = value
object.__setattr__(self, 'x', None)
object.__setattr__(self, 'position', old)
elif name == 'y':
old = self.position
old[1] = value
object.__setattr__(self, 'y', None)
object.__setattr__(self, 'position', old)
elif name == 'z':
old = self.position
old[2] = value
object.__setattr__(self, 'z', None)
object.__setattr__(self, 'position', old)
else:
object.__setattr__(self, name, value)