Jump to content

convert The Unit Circle to a bearing-- Math help


Recommended Posts

I need a bit of math help with Autoit, my math skills are embarrassing. I am trying to convert The Unit Circle to a bearing / angle. I have searched the forms and cant find what Im looking for. I did some research on the web and found the formula but Im not sure how to use them with Autoits math functions.

I would be grateful for any help thx. :)

Link to comment
Share on other sites

I did my Trig crash course online. Im not sure how I got it working but it does what I need now. Its not a prettiest script but it works. :(xyplane.au3

Not sure if it's what you are trying to do, but we had a thread earlier about uses for ATAN() and I wrote a function to take Relative XY coordinates and translate them to polar (theta and radius):

; Test _xy2polar() UDF
; PsaltyDS at AutoIT forums, 2006-01-26

While (1)
    $InputX = InputBox("Trig Test", "Enter value for X: ")
    If $InputX = "" Then Exit
    $InputY = InputBox("Trig Test", "Enter value for value for Y: ")
    If $InputY = "" Then Exit
    $Results = _xy2polar($InputX, $InputY)
    $SavError = @error
    If IsArray($Results) Then
        If MsgBox(32 + 1, "Function _xy2polar()", "Using the following inputs:" & @CRLF & _
                @TAB & "X = " & $InputX & @CRLF & _
                @TAB & "Y = " & $InputY & @CRLF & @CRLF & _
                "Calculated the following values:" & @CRLF & _
                @TAB & "R = " & $Results[0] & @CRLF & _
                @TAB & "Theta(Radians) = " & $Results[1] & @CRLF & _
                @TAB & "Theta(Degrees) = " & $Results[2]) = 2 Then ExitLoop
    Else
        MsgBox(16, "XY-2-Polar Error", "Error returned from _xy2polar() function, @Error = " & $SavError)
    EndIf
WEnd

; User Declared Function (UDF) _xy2polar() - calculates polar coordinates (r, Theta) from cartesean (x, y) coordinates
;   Usage:  _xy2polar($x, $y)  where:  $x and $y are signed X and Y coordinates (int or float) with a 0,0 center
;   Returns:  Three element one-dimensional array where:
;               [0] = Calculated radius from 0,0
;               [1] = Theta angle in radians, 0 <= Theta < 2(Pi)
;               [2] = Theta angle in degrees, 0 <= Theta < 360
;   On error:  Returns "", and @error = 1

Func _xy2polar($x, $y)
    $Pi = 4 * ATan(1)
    If StringIsInt($x) Or StringIsFloat($x) Then
        If StringIsInt($y) Or StringIsFloat($y) Then
            ; OK
        Else
            SetError(1)
            Return ""
        EndIf
    Else
        SetError(1)
        Return ""
    EndIf
    Dim $PolarAnswer[3] ; $Results[0]=Radius, [1]=Theta (in Radians), [2]=Theta (in Degrees)
    $r = Sqrt(($x ^ 2) + ($y ^ 2))
    $PolarAnswer[0] = $r
    Select
        Case $x = 0 And $y = 0
            $ThetaRad = 0
            $ThetaDeg = 0
        Case $x >= 0 And $y >= 0 ; +x/+y = 0-90deg quadrant
            $ThetaRad = ATan($x / $y)
            $ThetaDeg = $ThetaRad * 180 / $Pi
        Case $x >= 0 And $y < 0 ; +x/-y = 90-180deg quadrant
            $ThetaRad = ATan(Abs($y) / $x)
            $ThetaDeg = $ThetaRad * 180 / $Pi + 90
            $ThetaRad = $ThetaRad + $Pi / 2
        Case $x < 0 And $y < 0 ; -x/-y = 180-270deg quadrant
            $ThetaRad = ATan(Abs($x) / Abs($y))
            $ThetaDeg = $ThetaRad * 180 / $Pi + 180
            $ThetaRad = $ThetaRad + $Pi
        Case $x < 0 And $y >= 0 ; -x/+y = 2700-360deg quadrant
            $ThetaRad = ATan($y / Abs($x))
            $ThetaDeg = $ThetaRad * 180 / $Pi + 270
            $ThetaRad = $ThetaRad + 3 * $Pi / 2
    EndSelect
    $PolarAnswer[1] = $ThetaRad
    $PolarAnswer[2] = $ThetaDeg
    SetError(0)
    Return $PolarAnswer
EndFunc   ;==>_xy2polar

Cheers! :)

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...