Dear Emanuelle,
I used to have recurring problems with a Visual Basic program that relied
on the atan function to plot vectors . I finally resorted to BFI (brute
force & ignorance) and wrote a robust routine for calculating angles. Maybe
you would like to rewrite it for {Compute}.
Function Angle2(X, Y)
' Returns the angle subtended by two Cartesian coordinates;
' range = 0 to 2 * pi.
If X = 0 And Y = 0 Then
Angle2 = 0
Exit Function
End If
If X = 0 And Y > 0 Then
Angle2 = pi# / 2#
Exit Function
ElseIf X = 0 And Y < 0 Then
Angle2 = 3# * pi# / 2#
Exit Function
End If
If Y = 0 And X > 0 Then
Angle2 = 0#
Exit Function
ElseIf Y = 0 And X < 0 Then
Angle2 = pi#
Exit Function
End If
Angle2 = Atn(Y / X)
Select Case Quadrant(X, Y)
Case 1
Case 2
If Angle2 < 0 Then Angle2 = Angle2 + pi#
Case 3
Angle2 = Angle2 + pi#
Case 4
Angle2 = Angle2 + 2 * pi#
End Select
End Function
Function Quadrant(X, Y) As Integer
' (0,0) is assigned to quadrant 1 by default.
If X >= 0# Then
If Y >= 0# Then
Quadrant = 1
Else
Quadrant = 4
End If
Else
If Y >= 0# Then
Quadrant = 2
Else
Quadrant = 3
End If
End If
End Function
At 01:08 AM 8/01/05, you wrote:
Hi,
I'm triying to transform a vector field which is expressed with u and v
componants in the same vector field expressed with velocity and
direction.I have difficulties to obtain the direction.
...
This is my Compute expression :
(a.x>0 && a.y>=0) ? (180+90-atan(a.y/a.x)/deg) : ((a.x<0 && a.y>=0) ?
(90+atan(a.y/(-a.x))/deg) : ((a.x<0 && a.y<0) ?
(90-atan((-a.y)/(-a.x))/deg) : ((a.x>0 &&a.y<0) ?
(270+atan((-a.y)/a.x)/deg) : (a.x==0 && a.y>0) ? 180 : 0)))
where deg = 3.1415926535 / 180.0
Can anybody help me?
Allen H. Nugent
Graduate School of Biomedical Engineering
University of New South Wales
Sydney NSW 2052 Australia
Tel: +61 2 9385 3916 Fax: +61 2 9663 2108