Advertisement
Guest User

Untitled

a guest
Dec 10th, 2011
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.06 KB | None | 0 0
  1. """
  2. Helper class and functions for basic 3d vector math.
  3. """
  4.  
  5. class Vector3(object):
  6. def __init__(self,*point):
  7. """Define a vector in 3d space."""
  8. self.point = point
  9. def __add__(self,other):
  10. return map(reduce(lambda a,b: a+b),zip(self.point,other.point))
  11. def __sub__(self,other):
  12. return map(reduce(lambda a,b: a-b),zip(self.point,other.point))
  13. def __mul__(self,other):
  14. """Scalar multiplication aka the dot product."""
  15. return map(reduce(lambda a,b: a*b),zip(self.point,other.point))
  16. def __div__(self,other):
  17. return map(reduce(lambda a,b: a/b),zip(self.point,other.point))
  18. @property
  19. def x(self):
  20. return point[0]
  21. @property
  22. def y(self):
  23. return point[1]
  24. @property
  25. def z(self):
  26. return point[2]
  27.  
  28. def cross(a,b):
  29. """Vector multiplication aka the cross product."""
  30. x = a.y*b.z - a.z*b.y
  31. y = a.x*b.z - a.z*b.x
  32. z = a.x*b.y - a.y*b.x
  33. return Vector3(x,y,z)
  34.  
  35. print "Module py.vector3 loaded successfully."
  36.  
  37.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement