summaryrefslogtreecommitdiff
path: root/vector.mod/vector.bmx
diff options
context:
space:
mode:
Diffstat (limited to 'vector.mod/vector.bmx')
-rw-r--r--vector.mod/vector.bmx119
1 files changed, 110 insertions, 9 deletions
diff --git a/vector.mod/vector.bmx b/vector.mod/vector.bmx
index c2ca8a4..264f300 100644
--- a/vector.mod/vector.bmx
+++ b/vector.mod/vector.bmx
@@ -7,29 +7,130 @@ ModuleInfo "Version: $Revision$"
' $Id$
-Import brl.Basic
-
Strict
+Import brl.math
Type TVector
- ' These are considered private
- '
- Field v:Float[4,4]
+ Field x:Float
+ Field y:Float
+ Field z:Float
+
' Creates a new vector
'
- ' x - X componenent
- ' y - Y componenent
- ' z - Z componenent
+ ' dx - X componenent
+ ' dy - Y componenent
+ ' dz - Z componenent
'
' Returns null for invalid parameters.
'
- Function Create:TVector(x:Float, y:Float, z:Float)
+ Function Create:TVector(dx:Float, dy:Float, dz:Float)
Local o:TVector
o=New TVector
+ o.x=dx
+ o.y=dy
+ o.z=dz
+
Return o
End Function
+
+ ' Is this vector null
+ '
+ Method IsNull:Int()
+ Return x=0 And y=0 And z=0
+ End Method
+
+ ' Get the length of the vector
+ '
+ Method Length:Float()
+ If (IsNull())
+ Return 0
+ Else
+ Return Sqr(x*x+y*y+z*z)
+ EndIf
+ End Method
+
+ ' Normalise the vector
+ '
+ Method Normalise()
+ Local l:Float=Length()
+ If (l<>0)
+ x:/l
+ y:/l
+ z:/l
+ EndIf
+ End Method
+
+ ' Add a scalar to the vector
+ '
+ Method AddScalar(sc:Float)
+ x:+sc
+ y:+sc
+ z:+sc
+ End Method
+
+ ' Flip the vector
+ '
+ Method Minus()
+ x=-x
+ y=-y
+ z=-z
+ End Method
+
+ ' Scale the vector by a scalar
+ '
+ Method Scale(sc:Float)
+ x:*sc
+ y:*sc
+ z:*sc
+ End Method
+
+ ' Add another vector to this
+ '
+ Method Add(v:TVector)
+ x:+v.x
+ y:+v.y
+ z:+v.z
+ End Method
+
+ ' Subtract another vector from this
+ '
+ Method Subtract(v:TVector)
+ x:-v.x
+ y:-v.y
+ z:-v.z
+ End Method
+
+ ' Scale this with another vector
+ '
+ Method ScaleVector(v:TVector)
+ x:*v.x
+ y:*v.y
+ z:*v.z
+ End Method
+
+ ' Inverse scale this with another vector
+ '
+ Method InverseScaleVector(v:TVector)
+ x:/v.x
+ y:/v.y
+ z:/v.z
+ End Method
+
+ ' Calculate the cross product of this vector with another.
+ ' Returns a new vector.
+ '
+ 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
+
+ ' Calculate the dot product of this vector with another.
+ '
+ Method Dot:Float(v:TVector)
+ Return x*v.x + y*v.y + z*v.z
+ End Method
+
End Type