'Class module
Sub Class_Globals
Private Vector2DX As Double
Private Vector2DY As Double
End Sub
'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize (x As Double, y As Double)
Vector2DX = x
Vector2DY = y
End Sub
' /**
' * Creates an exact copy of this Vector2D prob
' * @Return Vector2D A copy of this Vector2D
' */
Public Sub cloneVector As Vector2D
Dim newVector As Vector2D
newVector.Initialize(Vector2DX, Vector2DY)
Return newVector
End Sub
' /**
' * Is the vector's length = 1?
' * @Return Boolean If length Is 1, True, Else False.
' */
Public Sub isNormalized
If getlength = 1 Then
Return True
Else
Return False
End If
End Sub
' /**
' * Sets the length which will change x AND y, but Not the angle.
' */
Public Sub setlength(value As Double)
Dim Vector2Dangle As Double
Vector2Dangle = getAngle
Vector2DX = Cos(Vector2Dangle) * value
Vector2DY = Sin(Vector2Dangle) * value
If Abs(Vector2DX) < .00000001 Then Vector2DX = 0
If Abs(Vector2DY) < .00000001 Then Vector2DY = 0
End Sub
' /**
' * Returns the length of the vector.
' **/
Public Sub getlength
Return Sqrt(lengthSquared)
End Sub
' /**
' * Returns the length of this vector, before square root. Allows For a faster check.
' */
Public Sub lengthSquared
Return Vector2DX * Vector2DX + Vector2DY * Vector2DY
End Sub
' /**
' * Changes the angle of the vector. X AND Y will change, length stays the same.
' */
Public Sub setAngle(value As Double)
Dim len As Double
len = getlength
Vector2DX = Cos(value) * len
Vector2DY = Sin(value) * len
End Sub
' /**
' * Get the angle of this vector.
' **/
Public Sub getAngle
Return ATan2(Vector2DY, Vector2DX)
End Sub
' /**
' * Sets the vector's length to 1.
' * @Return Vector2D This vector.
' */
Public Sub normalize As Vector2D
If getlength = 0 Then
Vector2DX = 1
Return Me
End If
Dim len As Double
len = getlength
Vector2DX = Vector2DX / len
Vector2DY = Vector2DY / len
Return Me
End Sub
' /**
' * Sets the vector's length to len.
' * @param len The length To set it To.
' * @Return Vector2D This vector.
' */
Public Sub normalcate(len As Double)
setlength(len)
Return Me
End Sub
' /**
' * Calculate squared distance between vectors. Faster than distance.
' * @param vector2 The other vector.
' * @Return Number The squared distance between the vectors.
' */
Public Sub distSQ(vector2 As Vector2D) As Double
Dim dx As Double
Dim dy As Double
dx = vector2.Vector2DX - Vector2DX
dy = vector2.Vector2DY - Vector2DY
Return (dx * dx + dy * dy)
End Sub
' /**
' * Add a vector To this vector.
' * @param vector2 The vector To add To this one.
' * @Return Vector2D This vector.
' */
Public Sub add(vector2 As Vector2D) As Vector2D
Vector2DX = Vector2DX + vector2.Vector2DX
Vector2DY = Vector2DY + vector2.Vector2DY
Return Me
End Sub
' /**
' * Subtract a vector from this one.
' * @param vector2 The vector To subtract.
' * @Return Vector2D This vector.
' */
Public Sub subtract(vector2 As Vector2D) As Vector2D
Vector2DX = Vector2DX - vector2.Vector2DX
Vector2DY = Vector2DY - vector2.Vector2DY
Return Me
End Sub
' /**
' * Mutiplies this vector by another one.
' * @param scalar The scalar To multiply by.
' * @Return Vector2D This vector, multiplied.
' */
Public Sub multiply(scalar As Double) As Vector2D
Vector2DX = Vector2DX * scalar
Vector2DY = Vector2DY * scalar
Return Me
End Sub
' /**
' * Divide this vector by a scalar.
' * @param scalar The scalar To divide by.
' * @Return Vector2D This vector.
' */
Public Sub divide(scalar As Double) As Vector2D
Vector2DX = Vector2DX / scalar
Vector2DY = Vector2DY / scalar
Return Me
End Sub
' /**
' * Set AND get y component.
' */
Public Sub setY(value As Double)
Vector2DY = value
End Sub
Public Sub getY
Return Vector2DY
End Sub
' /**
' * set AND get x component.
' */
Public Sub setX(value As Double)
Vector2DX = value
End Sub
Public Sub getX
Return Vector2DX
End Sub