Advertisement
Guest User

Untitled

a guest
Nov 27th, 2011
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.28 KB | None | 0 0
  1. class Vector(object):
  2. __slots__ = ['__data']
  3. def __init__(self, x=0.0, y=0.0, z=0.0):
  4. # self.__data = [x, y, z]
  5. self.__data = [x, y, 0] # ignore z for now
  6.  
  7. def __getitem__(self, index):
  8. if index >= 0 and index <= 3:
  9. return self.__data[index]
  10. else:
  11. raise Exception("Invalid vector index to get")
  12.  
  13. def __setitem__(self, index, val):
  14. if index >= 0 and index <= 3:
  15. self.__data[index] = val
  16. else:
  17. raise Exception("Invalid vector index to set")
  18.  
  19. def __add__(self, other):
  20. Vector(map(lambda x, y: x+y, self, other))
  21.  
  22. def __sub__(self, other):
  23. Vector(map(lambda x, y: x-y, self, other))
  24.  
  25. def __mul__(self, val):
  26. print val.__type__
  27. return Vector(self.__data[0]*val, self.__data[1]*val, self.__data[2]*val)
  28.  
  29. def __div__(self, val):
  30. return Vector(self.__data[0]/val, self.__data[1]/val, self.__data[2]/val)
  31.  
  32. def __iadd__(self, other):
  33. self.__data[0] += other[0]
  34. self.__data[1] += other[1]
  35. self.__data[2] += other[2]
  36. return self
  37.  
  38. def __isub__(self, other):
  39. self.__data[0] -= other[0]
  40. self.__data[1] -= other[1]
  41. self.__data[2] -= other[2]
  42. return self
  43.  
  44. def __imul__(self, val):
  45. self.__data[0] *= val
  46. self.__data[1] *= val
  47. self.__data[2] *= val
  48. return self
  49.  
  50. def __idiv__(self, val):
  51. self.__data[0] /= val
  52. self.__data[1] /= val
  53. self.__data[2] /= val
  54. return self
  55.  
  56. def __repr__(self):
  57. return "<Vector [%.2f %.2f %.2f]>" % (self.__data[0], self.__data[1], self.__data[2])
  58.  
  59. def __str__(self):
  60. return "<Vector [%.2f %.2f %.2f]>" % (self.__data[0], self.__data[1], self.__data[2])
  61.  
  62. def getLength(self):
  63. return (self.__data[0]**2 + self.__data[1]**2 + self.__data[2]**2)**0.5
  64.  
  65. def distanceToCoords(self, x, y, z):
  66. return ((self.__data[0] - x)**2 + (self.__data[1] - y)**2 + (self.__data[2] - z)**2)**0.5
  67.  
  68. def distanceToVector(self, vec):
  69. return ((self.__data[0] - vec[0])**2 + (self.__data[1] - vec[1])**2 + (self.__data[2] - vec[2])**2)**0.5
  70.  
  71. def getCoords(self):
  72. return self.__data
  73.  
  74.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement