# jch 31-OCT-2005; created TinyVector.py from Point.py utility # $Header: /jch/play/cvsroot/twist/Point.py,v 1.2 2003/05/30 03:20:04 jch Exp $ #// Copyright 2003 YON - Jan C. Hardenbergh. Permission to copy is #// granted as long as this copyright and this URL are maintained: #// http://www.jch.com/NURBS/ # import Numeric import math class TinyVector: def __init__(self, value): self.data = [value[0], value[1], value[2]] def __getitem__(self, index): return self.data[index] def __add__(self, other): return TinyVector([self.data[0]+other[0], self.data[1]+other[1], self.data[2]+other[2]]) def __sub__(self, other): return TinyVector([self.data[0]-other[0], self.data[1]-other[1], self.data[2]-other[2]]) def __mul__(self, other): return TinyVector([self.data[0]*other, self.data[1]*other, self.data[2]*other]) def __div__(self, other): return TinyVector([self.data[0]/other, self.data[1]/other, self.data[2]/other]) def DistanceTo(self, other): x = self.data[0]-other[0] y = self.data[1]-other[1] z = self.data[2]-other[2] return math.sqrt(x*x+y*y+z*z) def Length(self): length = math.sqrt(self.data[0]*self.data[0]+self.data[1]*self.data[1]+self.data[2]*self.data[2]) return length def Normalized(self): length = math.sqrt(self.data[0]*self.data[0]+self.data[1]*self.data[1]+self.data[2]*self.data[2]) return self.__div__(length) def Dot(self,other): return self.data[0]*other[0]+self.data[1]*other[1]+self.data[2]*other[2] # a x b = (a2b3 - a3b2) i + (a3b1 - a1b3) j + (a1b2 - a2b1) k def Cross(self,other): x = self.data[1]*other[2]-self.data[2]*other[1] y = self.data[2]*other[0]-self.data[0]*other[2] z = self.data[0]*other[1]-self.data[1]*other[0] return TinyVector([x,y,z]) def Print(self): print "TinyVector : %g, %g, %g" % (self.data[0], self.data[1], self.data[2]) def Assign(self,x,y,z): self.data[0] = x self.data[1] = y self.data[2] = z def GetVertex(self): return self.data