Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #Calculate normal
- #a: pointList[0] -> pointList[1] is...
- a = vector3.sub(pointList[0], pointList[1])
- #b: pointList[1] -> pointList[2] is...
- b = vector3.sub(pointList[1], pointList[2])
- # a ^ b / |a ^ b| is...
- normal = vector3.unit( vector3.cross(a,b) )
- #Displacement is...
- displacement = vector3.scale(normal,displacement)
- ...where...
- """
- Helper functions for basic 3d vector math.
- """
- import math
- def add(a,b):
- return map(sum, zip(a,b))
- def sub(a,b):
- return [ x-y for x,y in zip(a,b) ]
- def dot(a,b):
- """Scalar multiplication aka the dot product."""
- return [ x*y for x,y in zip(a,b) ]
- def cross(a,b):
- """Vector multiplication aka the cross product."""
- x = a[1]*b[2] - a[2]*b[1]
- y = a[0]*b[2] - a[2]*b[0]
- z = a[0]*b[1] - a[1]*b[0]
- return (x,y,z)
- def scale(vector,d):
- """Scale a vector a by the scalar value d."""
- return [ float(a)*d for a in vector ]
- def abs(vector):
- """Returns the magnitude of the given vector."""
- (a,b,c) = vector
- return math.sqrt(a**2+b**2+c**2)
- def unit(vector):
- """Returns a unit vector in the given direction."""
- mag = abs(vector)
- return scale(vector,1/mag)
- print "Module py.vector3 loaded successfully."
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement