Attribute VB_Name = "Vec2D"

Global Const PI = 3.141592


Public Type Vector2D
    x As Single
    y As Single
    r As Single
    theta As Double
End Type


Public Function InitVector2D_XY(x As Single, y As Single) As Vector2D
On Error Resume Next
Dim theta_temp As Double
    InitVector2D_XY.x = x
    InitVector2D_XY.y = y
    InitVector2D_XY.r = Sqr(x * x + y * y)
    theta_temp = (Atn(y / x))
    If y < 0 Then
        If x < 0 Then
            theta_temp = (theta_temp + PI)
        Else
            theta_temp = ((theta_temp) + 2 * PI)
        End If
    Else
        If x < 0 Then
            theta_temp = (theta_temp + PI)
        Else
            theta_temp = ((theta_temp) + 2 * PI)
        End If
    End If
    theta_temp = ((theta_temp * 180 / PI) Mod 360) * PI / 180
    InitVector2D_XY.theta = theta_temp
End Function

Public Function InitVector2D_RT(r As Single, theta As Double) As Vector2D
InitVector2D_RT.r = r
InitVector2D_RT.theta = ((theta * 180 / PI + 360) Mod 360) * PI / 180
InitVector2D_RT.x = r * Cos(theta)
InitVector2D_RT.y = r * Sin(theta)
End Function

Public Function InitVector2D_COPY(mag As Single, Dir As Vector2D) As Vector2D
InitVector2D_COPY = InitVector2D_RT((mag), Dir.theta)
End Function

Public Function negative(vec As Vector2D) As Vector2D
negative = InitVector2D_XY(-vec.x, -vec.y)
End Function

Public Function AddVec(v1 As Vector2D, v2 As Vector2D) As Vector2D
AddVec = InitVector2D_XY(v1.x + v2.x, v1.y + v2.y)
End Function

Public Function SubVec(v1 As Vector2D, v2 As Vector2D) As Vector2D
SubVec = AddVec(v1, negative(v2))
End Function

Public Function Dot(v1 As Vector2D, v2 As Vector2D) As Double
Dot = v1.x * v2.x + v1.y * v2.y
End Function

Public Function Perpendicular(vec As Vector2D) As Vector2D
Perpendicular = InitVector2D_RT(vec.r, vec.theta + PI / 2)
End Function
