# This module defines 3d geometrical vectors with the standard # operations on them. # # Written by: Konrad Hinsen # Last revision: 1996-1-26 # """This module defines three-dimensional geometrical vectors. Vectors support the usual mathematical operations (v1, v2: vectors, s: scalar): v1+v2 addition v1-v2 subtraction v1*v2 scalar product s*v1 multiplication with a scalar v1/s division by a scalar v1.cross(v2) cross product v1.length() length v1.normal() normal vector in direction of v1 v1.angle(v2) angle between two vectors v1.x(), v1[0] first element v1.y(), v1[1] second element v1.z(), v1[2] third element The module offers the following items for export: Vector(x,y,z) the constructor for vectors isVector(x) a type check function ex, ey, ez unit vectors along the x-, y-, and z-axes (predefined constants) Note: vector elements can be any kind of numbers on which the operations addition, subtraction, multiplication, division, comparison, sqrt, and acos are defined. Integer elements are treated as floating point elements. """ import umath, types class Vector: isVector = 1 def __init__(self, x=0., y=0., z=0.): self.data = [x,y,z] def __repr__(self): return 'Vector(%s,%s,%s)' % (`self.data[0]`,\ `self.data[1]`,`self.data[2]`) def __str__(self): return `self.data` def __add__(self, other): return Vector(self.data[0]+other.data[0],\ self.data[1]+other.data[1],self.data[2]+other.data[2]) __radd__ = __add__ def __neg__(self): return Vector(-self.data[0], -self.data[1], -self.data[2]) def __sub__(self, other): return Vector(self.data[0]-other.data[0],\ self.data[1]-other.data[1],self.data[2]-other.data[2]) def __rsub__(self, other): return Vector(other.data[0]-self.data[0],\ other.data[1]-self.data[1],other.data[2]-self.data[2]) def __mul__(self, other): if isVector(other): return reduce(lambda a,b: a+b, map(lambda a,b: a*b, self.data, other.data)) else: return Vector(self.data[0]*other, self.data[1]*other, self.data[2]*other) def __rmul__(self, other): if isVector(other): return reduce(lambda a,b: a+b, map(lambda a,b: a*b, self.data, other.data)) else: return Vector(other*self.data[0], other*self.data[1], other*self.data[2]) def __div__(self, other): if isVector(other): raise TypeError, "Can't divide by a vector" else: return Vector(_div(self.data[0],other), _div(self.data[1],other), _div(self.data[2],other)) def __rdiv__(self, other): raise TypeError, "Can't divide by a vector" def __cmp__(self, other): return cmp(self.data[0],other.data[0]) \ or cmp(self.data[1],other.data[1]) \ or cmp(self.data[2],other.data[2]) def __getitem__(self, index): return self.data[index] def x(self): return self.data[0] def y(self): return self.data[1] def z(self): return self.data[2] def length(self): return umath.sqrt(self*self) def normal(self): len = self.length() if len == 0: raise ZeroDivisionError, "Can't normalize a zero-length vector" return self/len def cross(self, other): if not isVector(other): raise TypeError, "Cross product with non-vector" return Vector(self.data[1]*other.data[2]-self.data[2]*other.data[1], self.data[2]*other.data[0]-self.data[0]*other.data[2], self.data[0]*other.data[1]-self.data[1]*other.data[0]) def angle(self, other): if not isVector(other): raise TypeError, "Angle between vector and non-vector" cosa = (self*other)/(self.length()*other.length()) cosa = max(-1.,min(1.,cosa)) return umath.acos(cosa) # Type check def isVector(x): return hasattr(x,'isVector') # "Correct" division for arbitrary number types def _div(a,b): if type(a) == types.IntType and type(b) == types.IntType: return float(a)/float(b) else: return a/b # Some useful constants ex = Vector(1.,0.,0.) ey = Vector(0.,1.,0.) ez = Vector(0.,0.,1.)