How to calculate arctangents in all four quadrants
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 | 'Math - How to calculate arctangents in all four quadrants 'Create a new project with a form and place a label (label1) on that. ' 'And place the following code: Option Explicit Private Sub Form_Load() AutoRedraw = True ForeColor = vbRed Line (0, ScaleHeight / 2)-Step(ScaleWidth, 0) Line (ScaleWidth / 2, 0)-Step(0, ScaleHeight) ForeColor = vbBlack Picture = Image End Sub 'Atan2(): Four-Quadrant Arc-Tangent * ' 'Given the coördinates, {x, y}, of a point, this function returns the angle, 'in radians, between the positive x-axis and a line from the origin to the 'point. The point can be anywhere in the plane. Atan2(0,0) is undefined, but 'returns 0. The arguments are called by value so there is no chance of the 'function corrupting them. ' 'Invocation and Domain of the result: ' ' -pi < Atan2(y,x) <= +pi ' 'The function is invoked exactly as is the atan2() function supplied with the 'C and C++ runtime libraries. ' Public Function Atan2(ByVal Y As Double, ByVal X As Double) As Double Dim theta As Double If (Abs(X) < 0.0000001) Then If (Abs(Y) < 0.0000001) Then theta = 0# ElseIf (Y > 0#) Then theta = 1.5707963267949 Else theta = -1.5707963267949 End If Else theta = Atn(Y / X) If (X < 0) Then If (Y >= 0#) Then theta = 3.14159265358979 + theta Else theta = theta - 3.14159265358979 End If End If End If Atan2 = theta End Function Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single) Const PI = 3.14159265 Dim radians As Single Dim degrees As Single Cls Line (ScaleWidth / 2, ScaleHeight / 2)-(X, Y) ' Reverse the Y distance's sign to get a clockwise measurement. radians = Atan2(-(Y - ScaleHeight / 2), X - ScaleWidth / 2) degrees = radians / PI * 180 Label1.Caption = Format$(radians, "0.00") & ", " & Format$(degrees, "0.00") End Sub |
Tags: activex code, API, arctangents, calculate, four, Math, quadrants, tutorial, vb activex control, vb6 tutorial, visual basic 6 programming, visual basic 6 simple, visual basic net programming, visual basic net tutorial, visual basic source code examples