Advertisement
Guest User

Untitled

a guest
Dec 10th, 2011
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.25 KB | None | 0 0
  1.  
  2. #Calculate normal
  3. #a: pointList[0] -> pointList[1] is...
  4. a = vector3.sub(pointList[0], pointList[1])
  5. #b: pointList[1] -> pointList[2] is...
  6. b = vector3.sub(pointList[1], pointList[2])
  7. # a ^ b / |a ^ b| is...
  8. normal = vector3.unit( vector3.cross(a,b) )
  9. #Displacement is...
  10. displacement = vector3.scale(normal,displacement)
  11.  
  12. ...where...
  13.  
  14. """
  15. Helper functions for basic 3d vector math.
  16. """
  17.  
  18. import math
  19.  
  20. def add(a,b):
  21. return map(sum, zip(a,b))
  22.  
  23. def sub(a,b):
  24. return [ x-y for x,y in zip(a,b) ]
  25.  
  26. def dot(a,b):
  27. """Scalar multiplication aka the dot product."""
  28. return [ x*y for x,y in zip(a,b) ]
  29.  
  30. def cross(a,b):
  31. """Vector multiplication aka the cross product."""
  32. x = a[1]*b[2] - a[2]*b[1]
  33. y = a[0]*b[2] - a[2]*b[0]
  34. z = a[0]*b[1] - a[1]*b[0]
  35. return (x,y,z)
  36.  
  37. def scale(vector,d):
  38. """Scale a vector a by the scalar value d."""
  39. return [ float(a)*d for a in vector ]
  40.  
  41. def abs(vector):
  42. """Returns the magnitude of the given vector."""
  43. (a,b,c) = vector
  44. return math.sqrt(a**2+b**2+c**2)
  45.  
  46. def unit(vector):
  47. """Returns a unit vector in the given direction."""
  48. mag = abs(vector)
  49. return scale(vector,1/mag)
  50.  
  51. print "Module py.vector3 loaded successfully."
  52.  
  53.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement