' Copyright (c) 2006 Ian Cowburn ' ' Permission is hereby granted, free of charge, to any person obtaining a copy of ' this software and associated documentation files (the "Software"), to deal in ' the Software without restriction, including without limitation the rights to ' use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies ' of the Software, and to permit persons to whom the Software is furnished to do ' so, subject to the following conditions: ' ' The above copyright notice and this permission notice shall be included in all ' copies or substantial portions of the Software. ' ' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR ' IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, ' FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE ' AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER ' LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, ' OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE ' SOFTWARE. ' ' $Id$ ' Rem bbdoc: noddybox.vector EndRem Module noddybox.vector ModuleInfo "Framework: Simple Vector class" ModuleInfo "Copyright: Ian Cowburn -- released under the MIT License" ModuleInfo "Author: Ian Cowburn" ModuleInfo "Version: $Revision$" Strict Import brl.math Rem bbdoc: Implements a 3D vector. Can be used as a 2D vector by ignoring Z. EndRem Type TVector Rem bbdoc: The X component of the vector. EndRem Field x:Double Rem bbdoc: The Y component of the vector. EndRem Field y:Double Rem bbdoc: The Z component of the vector. EndRem Field z:Double Rem bbdoc: Create a vector. returns: The created vector. about: @dx, @dy and @dz are the initial component values. EndRem Function Create:TVector(dx:Double=0.0, dy:Double=0.0, dz:Double=0.0) Local o:TVector o=New TVector o.x=dx o.y=dy o.z=dz Return o End Function Rem bbdoc: Clone this vector to a new object. returns: The created vector. EndRem Method Clone:TVector() Return Create(x,y,z) End Method Rem bbdoc: Is the vector null. returns: True if the vector is null (all components zero), False otherwise. EndRem Method IsNull:Int() Return x=0 And y=0 And z=0 End Method Rem bbdoc: The length of the vector. returns: The length of the vector. EndRem Method Length:Double() If (IsNull()) Return 0 Else Return Sqr(x*x+y*y+z*z) EndIf End Method Rem bbdoc: Normalise the vector so its length is 1. EndRem Method Normalise() Local l:Double=Length() If (l<>0) x:/l y:/l z:/l EndIf End Method Rem bbdoc: Set the vector so its length is @sc. EndRem Method SetLength(sc:Double) Normalise() Scale(sc) End Method Rem bbdoc: Adds @sc to all components. EndRem Method AddScalar(sc:Double) x:+sc y:+sc z:+sc End Method Rem bbdoc: Reverse the direction of the vector. EndRem Method Minus() x=-x y=-y z=-z End Method Rem bbdoc: Scale all components by @sc. EndRem Method Scale(sc:Double) x:*sc y:*sc z:*sc End Method Rem bbdoc: Add another vector to this vector. EndRem Method Add(v:TVector) x:+v.x y:+v.y z:+v.z End Method Rem bbdoc: Subtract another vector from this vector. EndRem Method Subtract(v:TVector) x:-v.x y:-v.y z:-v.z End Method Rem bbdoc: Scale this vector by another vector. EndRem Method ScaleVector(v:TVector) x:*v.x y:*v.y z:*v.z End Method Rem bbdoc: Inverse scale this vector by another vector. EndRem Method InverseScaleVector(v:TVector) x:/v.x y:/v.y z:/v.z End Method Rem bbdoc: Calculate the cross product of this vector with another. returns: A new @TVector holding the cross product. EndRem Method Cross:TVector(v:TVector) Return TVector.Create(y * v.z - v.y * z, z * v.x - v.z * x, x * v.y - v.x * y) End Method Rem bbdoc: Calculate the dot product of this vector with another. returns: The dot product. EndRem Method Dot:Double(v:TVector) Return x*v.x + y*v.y + z*v.z End Method End Type