import math class Vec3D: """Class to represent a vectir in three-space""" def __init__(self,x,y,z): """Constructor for a Vec3D class""" self.data = [x,y,z] def x(self): return self.data[0] def y(self): return self.data[1] def z(self): return self.data[2] def __getitem__(self,key): if key < 0 or key > 2: raise IndexError return self.data[key] def __setitem__(self,key,value): if key < 0 or key > 2: raise IndexError self.data[key] = value def __iadd__(self,other): if isinstance(other,Vec3D): self.data[0] += other.data[0] self.data[1] += other.data[1] self.data[2] += other.data[2] else: self.data[0] += other self.data[1] += other self.data[2] += other return(self) def __isub__(self,other): if isinstance(other,Vec3D): self.data[0] -= other.data[0] self.data[1] -= other.data[1] self.data[2] -= other.data[2] else: self.data[0] -= other self.data[1] -= other self.data[2] -= other return(self) def __imul__(self,other): self.data[0] *= other self.data[1] *= other self.data[2] *= other return(self) def __idiv__(self,other): self.data[0] /= other self.data[1] /= other self.data[2] /= other return(self) def Magnitude(self): """Computes the manitude of the vector""" return math.sqrt(self.data[0]*self.data[0] + self.data[1]*self.data[1] + self.data[2]*self.data[2]) def Normalize(self): """Returns a normalized version of the vector""" len = self.Magnitude() return Vec3D(self.data[0]/len,self.data[1]/len, self.data[2]/len) def __neg__(self): return Vec3D(-self.data[0],-self.data[1],-self.data[2]) def __cmp__(self,other): if ((self.data[0] == other.data[0]) and (self.data[1] == other.data[1]) and (self.data[2] == other.data[2])): return 0 mself = (self.data[0]*self.data[0] + self.data[1]*self.data[1] + self.data[2]*self.data[2]) mother = (other.data[0]*other.data[0] + other.data[1]*other.data[1] + other.data[2]*other.data[2]) if mself < mother: return -1 return 1 def __add__(self,other): if isinstance(other,Vec3D): return Vec3D(self.data[0]+other.data[0], self.data[1]+other.data[1], self.data[2]+other.data[2]) else: return Vec3D(self.data[0]+other, self.data[1]+other, self.data[2]+other) def __sub__(self,other): if isinstance(other,Vec3D): return Vec3D(self.data[0]-other.data[0], self.data[1]-other.data[1], self.data[2]-other.data[2]) else: return Vec3D(self.data[0]-other, self.data[1]-other, self.data[2]-other) def __rsub__(self,other): return Vec3D(other-self.data[0], other-self.data[1], other-self.data[2]) def __mul__(self,other): if isinstance(other,Vec3D): return (self.data[0]*other.data[0] + self.data[1]*other.data[1] + self.data[2]*other.data[2]) else: return Vec3D(self.data[0]*other, self.data[1]*other, self.data[2]*other) def __rmul__(self,other): return Vec3D(self.data[0]*other, self.data[1]*other, self.data[2]*other) def __div__(self,other): return Vec3D(self.data[0]/other, self.data[1]/other, self.data[2]/other) def __repr__(self): return "%g %g %g" % (self.data[0],self.data[1],self.data[2]) def CrossProd(op1,op2): return Vec3D((op1.y()*op2.z()) - (op1.z()*op2.y()), (op1.z()*op2.x()) - (op1.x()*op2.z()), (op1.x()*op2.y()) - (op1.y()*op2.x())) def TripleProd(op1,op2,op3): tmp = CrossProd(op1,op2) return tmp * op3 def Max(op1,op2): if op1.x() >= op2.x(): x = op1.x() else: x = op2.x() if op1.y() >= op2.y(): y = op1.y() else: y = op2.y() if op1.z() >= op2.z(): z = op1.z() else: z = op2.z() return Vec3D(x,y,z) ; def Min(op1,op2): if op1.x() <= op2.x(): x = op1.x() else: x = op2.x() if op1.y() <= op2.y(): y = op1.y() else: y = op2.y() if op1.z() <= op2.z(): z = op1.z() else: z = op2.z() return Vec3D(x,y,z) ;