summaryrefslogtreecommitdiff
path: root/vector.mod/vector.bmx
blob: 67818fe31485b0a7b64cf85e76bcf046f2b2c714 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
Rem
bbdoc: noddybox.vector
EndRem
Module noddybox.vector

ModuleInfo "Framework: Simple Vector class"
ModuleInfo "Copyright: Public Domain"
ModuleInfo "Author: Ian Cowburn"
ModuleInfo "Version: $Revision$"

' $Id$

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:Float
	Rem
	bbdoc: The Y component of the vector.
	EndRem
	Field y:Float
	Rem
	bbdoc: The Z component of the vector.
	EndRem
	Field z:Float


	Rem
	bbdoc: Create a vector.
	returns: The created vector.
	about: @dx, @dy and @dz are the initial component values.
	EndRem
	Function Create:TVector(dx:Float=0.0, dy:Float=0.0, dz:Float=0.0)
		Local o:TVector
		
		o=New TVector
		
		o.x=dx
		o.y=dy
		o.z=dz
		
		Return o
	End Function
	
	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:Float()
		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:Float=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:Float)
		Normalise()
		Scale(sc)
	End Method
	
	Rem
	bbdoc: Adds @sc to all components.
	EndRem
	Method AddScalar(sc:Float)
		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:Float)
		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:Float(v:TVector)
		Return x*v.x + y*v.y + z*v.z
	End Method

End Type