Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Vector(object):
- __slots__ = ['__data']
- def __init__(self, x=0.0, y=0.0, z=0.0):
- # self.__data = [x, y, z]
- self.__data = [x, y, 0] # ignore z for now
- def __getitem__(self, index):
- if index >= 0 and index <= 3:
- return self.__data[index]
- else:
- raise Exception("Invalid vector index to get")
- def __setitem__(self, index, val):
- if index >= 0 and index <= 3:
- self.__data[index] = val
- else:
- raise Exception("Invalid vector index to set")
- def __add__(self, other):
- Vector(map(lambda x, y: x+y, self, other))
- def __sub__(self, other):
- Vector(map(lambda x, y: x-y, self, other))
- def __mul__(self, val):
- print val.__type__
- return Vector(self.__data[0]*val, self.__data[1]*val, self.__data[2]*val)
- def __div__(self, val):
- return Vector(self.__data[0]/val, self.__data[1]/val, self.__data[2]/val)
- def __iadd__(self, other):
- self.__data[0] += other[0]
- self.__data[1] += other[1]
- self.__data[2] += other[2]
- return self
- def __isub__(self, other):
- self.__data[0] -= other[0]
- self.__data[1] -= other[1]
- self.__data[2] -= other[2]
- return self
- def __imul__(self, val):
- self.__data[0] *= val
- self.__data[1] *= val
- self.__data[2] *= val
- return self
- def __idiv__(self, val):
- self.__data[0] /= val
- self.__data[1] /= val
- self.__data[2] /= val
- return self
- def __repr__(self):
- return "<Vector [%.2f %.2f %.2f]>" % (self.__data[0], self.__data[1], self.__data[2])
- def __str__(self):
- return "<Vector [%.2f %.2f %.2f]>" % (self.__data[0], self.__data[1], self.__data[2])
- def getLength(self):
- return (self.__data[0]**2 + self.__data[1]**2 + self.__data[2]**2)**0.5
- def distanceToCoords(self, x, y, z):
- return ((self.__data[0] - x)**2 + (self.__data[1] - y)**2 + (self.__data[2] - z)**2)**0.5
- def distanceToVector(self, vec):
- return ((self.__data[0] - vec[0])**2 + (self.__data[1] - vec[1])**2 + (self.__data[2] - vec[2])**2)**0.5
- def getCoords(self):
- return self.__data
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement