Hey Chroma..
I enhanced your code a little bit.. I hope you don't mind?!
I enhanced your code a little bit.. I hope you don't mind?!
Strict '// Vector Math Library v0.9 '// by Chroma '// rewritten, extended and OOP'ified for use in BlitzMax by LarsG '// Last Update: June 23, 2005 '// Comments: Enhanced and OOP'ified by LarsG '----------------------------------------------------------------------; '// Testing... Global vec1:TVectorF Global vec2:TVectorF '// Create two test vectors vec1 = TVectorF.Create(1,2,3) vec2 = TVectorF.Create() Print "Vec1: " Print vec1.show(", ") Print "vec2 cloning vec1..." vec2.clone(vec1) Print "Vec2: " Print vec2.show(", ") Print "Adding vec2 to vec1..." vec1.add(vec2) Print "Vec1: " Print vec1.Show(", ") End '//...End Test '----------------------------------------------------------------------; '// Vector Type Float (32 Bit) Type TVectorF Field x:Float Field y:Float Field z:Float Const TOL:Float = 0.001 '**** THE METHODS ***** Function Create:TVectorF(x:Float=0, y:Float=0, z:Float=0) Local v:TVectorF = New TVectorF v.x = x v.y = y v.z = z Return v EndFunction Method Set(x:Float, y:Float, z:Float) self.x = x self.y = y self.z = z EndMethod Method SetX(x:Float) self.x = x EndMethod Method SetY(y:Float) self.y = y EndMethod Method SetZ(z:Float) self.z = z EndMethod Method GetX:Float() Return self.x EndMethod Method GetY:Float() Return self.y EndMethod Method GetZ:Float() Return self.z EndMethod Method Add(plus:TVectorF) self.x :+ plus.x self.y :+ plus.y self.z :+ plus.z EndMethod Method AddScalar(scale:Float) self.x :+ scale self.y :+ scale self.z :+ scale EndMethod Method AddTimeStep(plus:TVectorF, tstep:Float) self.x :+ (plus.x * tstep) self.y :+ (plus.y * tstep) self.z :+ (plus.z * tstep) EndMethod Method Subtract(sub:TVectorF) self.x :- sub.x self.y :- sub.y self.z :- sub.z EndMethod Method SubtractScalar(scale:Float) self.x :- scale self.y :- scale self.z :- scale EndMethod Method SubtractTimeStep(sub:TVectorF, tstep:Float) self.x :- (sub.x * tstep) self.y :- (sub.y * tstep) self.z :- (sub.z * tstep) EndMethod Method DivideScalar(scale:Float) self.x :/ scale self.y :/ scale self.z :/ scale EndMethod Method CrossProduct(vec1:TVectorF, vec2:TVectorF) self.x = vec1.y * vec2.z - vec1.z * vec2.y self.y = -vec1.x * vec2.z + vec1.z * vec2.x self.z = vec1.x * vec2.y - vec1.y * vec2.x EndMethod Method DotProduct:Float(vec:TVectorF) Return vec.x * self.x + vec.y * self.y + vec.z * self.z EndMethod Method Magnitude:Float() Return Sqr(self.x * self.x + self.y * Self.y + self.z * self.z) EndMethod Method Normalize() Local magnitude:Float = self.Magnitude() self.x :/ magnitude self.y :/ magnitude self.z :/ magnitude If (Abs(self.x) < self.TOL) Then self.x = 0.0 If (Abs(self.y) < self.TOL) Then self.y = 0.0 If (Abs(self.z) < self.TOL) Then self.z = 0.0 EndMethod Method Reverse(vec:TVectorF) self.x = -vec.x self.y = -vec.y self.z = -vec.z EndMethod Method Reset() self.x = 0 self.y = 0 self.z = 0 EndMethod Method Clone(vec:TVectorF) ' not sure if it's neccesary to do it this way.. ' oh well.. it's there anyway ' maybe self = vec will do.. self.x = vec.x self.y = vec.y self.z = vec.z EndMethod Method Show:String(sep:String) Return String(self.x) + sep + .. String(self.y) + sep + .. String(self.z) EndMethod EndType '// Vector Type Double (64 Bit) Type TVectorD Field x:Double Field y:Double Field z:Double Const TOL:Double = 0.001 '**** THE METHODS ***** Function Create:TVectorD(x:Double=0, y:Double=0, z:Double=0) Local v:TVectorD = New TVectorD v.x = x v.y = y v.z = z Return v EndFunction Method Set(x:Double, y:Double, z:Double) self.x = x self.y = y self.z = z EndMethod Method SetX(x:Double) self.x = x EndMethod Method SetY(y:Double) self.y = y EndMethod Method SetZ(z:Double) self.z = z EndMethod Method GetX:Double() Return self.x EndMethod Method GetY:Double() Return self.y EndMethod Method GetZ:Double() Return self.z EndMethod Method Add(plus:TVectorD) self.x :+ plus.x self.y :+ plus.y self.z :+ plus.z EndMethod Method AddScalar(scale:Double) self.x :+ scale self.y :+ scale self.z :+ scale EndMethod Method AddTimeStep(plus:TVectorD, tstep:Double) self.x :+ (plus.x * tstep) self.y :+ (plus.y * tstep) self.z :+ (plus.z * tstep) EndMethod Method Subtract(sub:TVectorD) self.x :- sub.x self.y :- sub.y self.z :- sub.z EndMethod Method SubtractScalar(scale:Double) self.x :- scale self.y :- scale self.z :- scale EndMethod Method SubtractTimeStep(sub:TVectorD, tstep:Double) self.x :- (sub.x * tstep) self.y :- (sub.y * tstep) self.z :- (sub.z * tstep) EndMethod Method DivideScalar(scale:Double) self.x :/ scale self.y :/ scale self.z :/ scale EndMethod Method CrossProduct(vec1:TVectorD, vec2:TVectorD) self.x = vec1.y * vec2.z - vec1.z * vec2.y self.y = -vec1.x * vec2.z + vec1.z * vec2.x self.z = vec1.x * vec2.y - vec1.y * vec2.x EndMethod Method DotProduct:Double(vec:TVectorD) Return vec.x * self.x + vec.y * self.y + vec.z * self.z EndMethod Method Magnitude:Double() Return Sqr(self.x * self.x + self.y * Self.y + self.z * self.z) EndMethod Method Normalize() Local magnitude:Double = self.Magnitude() self.x :/ magnitude self.y :/ magnitude self.z :/ magnitude If (Abs(self.x) < self.TOL) Then self.x = 0.0 If (Abs(self.y) < self.TOL) Then self.y = 0.0 If (Abs(self.z) < self.TOL) Then self.z = 0.0 EndMethod Method Reverse(vec:TVectorD) self.x = -vec.x self.y = -vec.y self.z = -vec.z EndMethod Method Reset() self.x = 0 self.y = 0 self.z = 0 EndMethod Method Clone(vec:TVectorD) self.x = vec.x self.y = vec.y self.z = vec.z EndMethod Method Show:String(sep:String) Return String(self.x) + sep + .. String(self.y) + sep + .. String(self.z) EndMethod EndType