Attribute VB_Name = "MathStuff"
Private Declare Function IntersectRect Lib "user32" ( _
          lpDestRect As Rect, _
          lpSrc1Rect As Rect, _
          lpSrc2Rect As Rect) As Long
'Public Type Point
'    X As Long
'    Y As Long
'End Type
'Public Type Rect
'    Left As Long
'    Top As Long
'    Right As Long
'    Bottom As Long
'End Type

Const Pi = 3.141592654

Public Function FindAngle(XDif As Long, YDif As Long) As Long
'Finds the angle from Point A to Point B
Dim Distance As Single
Dim Vector As Long
    
    If XDif And YDif <> 0 Then
        Distance = Sqr((XDif * XDif) + (YDif * YDif))
        Vector = ArcSin(Abs(YDif / Distance))
        If XDif < 0 And YDif < 0 Then Vector = Vector + 180
        If XDif < 0 And YDif > 0 Then Vector = Vector + 270
        If XDif > 0 And YDif < 0 Then Vector = Vector + 90
    Else
        If XDif = 0 And YDif > 0 Then FindAngle = 0
        If YDif = 0 And XDif > 0 Then FindAngle = 90
        If XDif = 0 And YDif < 0 Then FindAngle = 180
        If YDif = 0 And XDif < 0 Then FindAngle = 270
        If YDif = 0 And XDif = 0 Then FindAngle = 0
        Exit Function
    End If
    
    'Make sure the value is between 0 and 360
    Do Until Vector <= 360 And Vector >= 0
        If Vector <= 0 Then Vector = Vector + 360
        If Vector > 360 Then Vector = Vector - 360
    Loop
    FindAngle = Vector
End Function

Public Function Deg(ByRef X)
'Convert radians to degrees
    
    Deg = X * (180 / Pi)
End Function

Public Function Rad(ByRef X)
'Convert degrees to radians
    
    Rad = X * (Pi / 180)
End Function

Public Function ArcSin(ByVal X As Single)
'Find angle from opposite and hypotenus
'X = o/h
    
    If Sqr(-X * X + 1) = 0 Then ArcSin = 0: Exit Function
    ArcSin = Deg(Atn(X / Sqr(-X * X + 1)))
End Function

Public Function ArcCos(ByVal X As Single)
'Find angle from adjacent and hypotenus
'X = a/h
    
    If Sqr(Abs(-X * X + 1)) = 0 Then ArcCos = 0: Exit Function
    ArcCos = Deg(Atn(-X / Sqr(Abs(-X * X + 1))) + 2 * Atn(1))
End Function

Public Sub Move(It As Point, ByVal Speed As Long, ByVal Direction As Long)
'Move towards an angle at a speed (2D)

    It.X = It.X + Speed * Sin(Rad(Direction))
    It.Y = It.Y + Speed * Cos(Rad(Direction))
End Sub

Public Function Within(ByRef Object1 As Rect, ByRef Object2 As Rect) As Boolean
'See if one is touching the other
Dim ReturnRect As Rect
          
          Within = CBool(IntersectRect(ReturnRect, Object1, Object2))
End Function

Public Sub Swap(ByRef N1 As Long, ByRef N2 As Long)
'Swap the value of 2 variables
Dim N3 As Long
    N3 = N1
    N1 = N2
    N2 = N3
End Sub
