Type Vector3
' Members
Field x:Float, y:Float, z:Float
' Set vector from x, y, z
Method FromXYZ(x:Float, y:Float, z:Float)
Self.x = x
Self.y = y
Self.z = z
EndMethod
' Set vector from Vector3
Method FromVector(v:Vector3)
Self.x = v.x
Self.y = v.y
Self.z = v.z
EndMethod
' Get coordinate from index
Method GetCoord:Float(Index:Int)
If Index = 0 Return x
If Index = 1 Return y
Return z
EndMethod
' Create new vector from x, y, z
Function CreateFromXYZ:Vector3(x:Float, y:Float, z:Float)
Local Result:Vector3 = New Vector3
Result.FromXYZ(x, y, z)
Return Result
EndFunction
' Create new vector from another
Function CreateFromVector:Vector3(v:Vector3)
Local Result:Vector3 = New Vector3
Result.FromVector(v)
Return Result
EndFunction
' Add two vectors
Function Add:Vector3(Result:Vector3, v1:Vector3, v2:Vector3)
Result.x = v1.x + v2.x
Result.y = v1.y + v2.y
Result.z = v1.z + v2.z
Return Result
EndFunction
' Subtract two vectors
Function Sub:Vector3(Result:Vector3, v1:Vector3, v2:Vector3)
Result.x = v1.x - v2.x
Result.y = v1.y - v2.y
Result.z = v1.z - v2.z
Return Result
EndFunction
' Reverse vector
Function Reverse:Vector3(Result:Vector3, v1:Vector3)
Result.x = -v1.x
Result.y = -v1.y
Result.z = -v1.z
Return Result
EndFunction
' Multiply vector with scalar
Function MulScalar:Vector3(Result:Vector3, v:Vector3, Scalar:Float)
Result.x = v.x * Scalar
Result.y = v.y * Scalar
Result.z = v.z * Scalar
Return Result
EndFunction
' Get squared length of vector
Function SquaredLength:Float(v:Vector3)
Return Dot(v, v)
EndFunction
' Get length of vector
Function Length:Float(v:Vector3)
Return Sqr(SquaredLength(v))
EndFunction
' Normalize vector
Function Normalize:Vector3(Result:Vector3, v:Vector3)
Local l:Float = Length(v)
If l = 0.0 Then
Result = v ' Avoid divide by zero by just returning self. Shouldn't happen anyway :)
Else
MulScalar(Result, v, 1.0 / l)
EndIf
Return Result
EndFunction
' Dot product of vector
Function Dot:Float(v1:Vector3, v2:Vector3)
Return (v1.x * v2.x + v1.y * v2.y + v1.z * v2.z);
EndFunction
' Cross product of vector
Function Cross:Vector3(Result:Vector3, v1:Vector3, v2:Vector3)
Result.x = v1.y * v2.z - v1.z * v2.y;
Result.y = v2.z * v2.x - v1.x * v2.z;
Result.z = v2.x * v2.y - v1.y * v2.x;
Return Result
EndFunction
' Interpolate two vectors
Function Interpolate:Vector3(Result:Vector3, v1:Vector3, v2:Vector3, t:Float)
Result.x = v1.x + t * (v2.x - v1.x);
Result.y = v1.y + t * (v2.y - v1.y);
Result.z = v1.z + t * (v2.z - v1.z);
Return Result
EndFunction
' Get distance between two points
Function GetDistance:Float(v1:Vector3, v2:Vector3)
Return Sqr( (v2.x - v1.x) * (v2.x - v1.x) + (v2.y - v1.y) * (v2.y - v1.y) + (v2.z - v1.z) * (v2.z - v1.z) )
EndFunction
EndType