An ArcTangent function that works in all quadrants

BillSoo
03-22-2002, 04:27 PM
One of the defects of the atn function is that it only works for angles in the first quadrant. For any other quadrant, you have to do some post processing to figure out which quadrant you are in, then add the required number of radians. Also, you have to be careful to avoid division by zero errors when trying to find the angle of a vertical line.

In C, you don't have this problem because the arctangent function there (I can't remember the name) accepts deltaX and deltaY parameters rather than just the ratio of the two. So here is a VB version of arctangent that also accepts 2 parameters, op (for opposite or deltaY) and ad (for adjacent or deltaX).


Public Function ArcTangent(ByVal op As Double, ByVal ad As Double) As Double
Const PI = 3.14159265358979
Dim at As Double

If ad = 0 Then
If op > 0 Then
ArcTangent = PI / 2#
Else
ArcTangent = PI * 1.5
End If
Else
at = Atn(op / ad)
If ad < 0 Then
ArcTangent = PI + at
ElseIf op# < 0 Then
ArcTangent = PI + PI + at
Else
ArcTangent = at
End If
End If
End Function

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum