Du bist hier: Snippet-Verzeichnis » Visual Basic (78)
Sprache:

Atn2

Sprache: English
Programmiersprache: Visual Basic
Veröffentlicht von: jdp [nicht registriert]
Letzte Änderung: 15.05.2006
Aufrufe: 1991


Beschreibung

This vb function calculates the inverse tangent and returns an angle between 0 and 2pi. It avoids the problems of y/zero and not knowing whether x or y is negative if y/x is negative when using the built in Atn function.

Code

1 'Usage: 2 3 Dim x as Double 4 Dim y as Double 5 Dim theta as Double 6 x=-1 7 y=1 8 theta=Atn2(x,y) 9 10 'results in theta=2.356... which is 3/4 pi 11 12 Private Function Atn2(x As Double, y As Double) As Double 13 14 'The Atn2 function takes the two sides of a right 15 'triangle (as double) and returns the corresponding angle 16 'in radians (as double). x is the length of the side adjacent 17 'to the angle and y is the length of the side opposite 18 'the angle. 19 20 'The range of the result is 0 to 2pi radians. 21 22 'To convert degrees to radians, multiply degrees by pi/180. 23 'To convert radians to degrees, multiply radians by 180/pi. 24 25 Const Pi = 3.14159265358979 26 Select Case x 27 Case Is > 0 28 Select Case y 29 Case Is > 0 30 Atn2 = Atn(y / x) 31 Case Is = 0 32 Atn2 = 0 33 Case Is < 0 34 Atn2 = Atn(y / x) + Pi + Pi 35 End Select 36 Case Is = 0 37 Select Case y 38 Case Is > 0 39 Atn2 = Pi / 2# 40 Case Is = 0 41 Atn2 = 0 42 Case Is < 0 43 Atn2 = Pi + Pi / 2# 44 End Select 45 Case Is < 0 46 Atn2 = Pi - Atn(-y / x) 47 End Select 48 End Function 49

Noch kein Kommentar vorhanden

Dieses Snippet kommentieren

Name *  

E-Mail (wird nicht angezeigt) *    

Website  

Kommentar *  

Sicherheitscode Sicherheitscode *    

RSS