Trig Functions in VB

Visual basic provides the following native trig functions, where a is in radians and Atn(x) returns a value in radians:

Sin(a)

Cos(a)

Tan(a)

Atn(x)  inverse tangent

So what happens when you need an inverse cosine or inverse sine?  In these cases, you can use trig identities to compute the missing function.  For example, the arcsin can be computed from the arctan as follows:

ArcSin(X) = Atn(X / Sqr(-X * X + 1))

ArcSin(X) will return a value in radians. You can use ArcSin, ArcCos and Atan2 as a function anywhere in your code if you cut and paste the following code to a module in your project:

Public Const Pi As Double = 3.14159265358979

Public Function ArcSin(x As Double) As Double
ArcSin = Atn(x / Sqr(-x * x + 1))
End Function

Public Function ArcCos(x As Double) As Double
ArcCos = Atn(-x / Sqr(-x * x + 1)) + 2 * Atn(1)
End Function

Public Function Atan2(ByVal x As Double, ByVal y As Double) As Double
'Calculates the angle in all four quadrants of a vector
If y > 0 Then
  If x >= y Then
    Atan2 = Atn(y / x)
  ElseIf x <= -y Then
    Atan2 = Atn(y / x) + Pi
  Else
    Atan2 = Pi / 2 - Atn(x / y)
  End If
Else
  If x >= -y Then
    Atan2 = Atn(y / x)
  ElseIf x <= y Then
    Atan2 = Atn(y / x) - Pi
  Else
    Atan2 = -Atn(x / y) - Pi / 2
  End If
End If
End Function

You can see a more complete list of trig identities in VB here:

http://en.wikibooks.org/wiki/Visual_Basic/Simple_Arithmetic