Jump to content

# Converting math from LUA to AutoIT

## Recommended Posts

Hi there, I am trying to conver the following LUA code to AutoIT:

`\$angle = (((math.atan2(deltaX,-1*deltaY) * 180 / math.pi) + 360) % 360)`

It returns the direction angle.

However, I do not understand how to work with the % 360 part.

Can anyone help me out?

Thanks!

Edit: now know the function Atan2 in math.au3 - only don''t understand the % 360 part, anyone who can help me with that?

Edit2: This is what I got so far, but how to deal with the % 360 thing? what does it do?

```#Include <Math.au3>
\$pi = 3.14159265358979
\$angle = ((_ATan2(\$deltaX, -1 * \$deltaY) * 180 / \$pi) + 360)```
Edited by tom13

#### Share this post

##### Share on other sites

Hi there, I am trying to conver the following LUA code to AutoIT:

`\$angle = (((math.atan2(deltaX,-1*deltaY) * 180 / math.pi) + 360) % 360)`

It returns the direction angle.

However, I do not understand how to work with the % 360 part.

Can anyone help me out?

Thanks!

Edit: now know the function Atan2 in math.au3 - only don''t understand the % 360 part, anyone who can help me with that?

Edit2: This is what I got so far, but how to deal with the % 360 thing? what does it do?

```#Include <Math.au3>
\$pi = 3.14159265358979
\$angle = ((_ATan2(\$deltaX, -1 * \$deltaY) * 180 / \$pi) + 360)```
The function _xy2polar() was one of my earliest AutoIt scripts I posted to the forum, here the function is adapted to a demo using left click of the mouse and presenting change from the last click:
```#include <Misc.au3>

HotKeySet("{ESC}", "_Quit")

Global \$iCurrPos[2], \$iLastPos[2], \$iDeltaX, \$iDeltaY

; Calculate polar coordinates between mouse left clicks
While 1
If _IsPressed("01") Then
\$iCurrPos = MouseGetPos()
\$iDeltaX = \$iCurrPos[0] - \$iLastPos[0]
\$iDeltaY = \$iCurrPos[1] - \$iLastPos[1]
\$avPolar = _xy2polar(\$iDeltaX, \$iDeltaY * - 1)
\$iLastPos = \$iCurrPos
TrayTip("Angle", "\$iDeltaX = " & \$iDeltaX & @LF & _
"\$iDeltaY = " & \$iDeltaY & @LF & _
"Radius = " & \$avPolar[0] & @LF & _
"Theta (Radians) = " & \$avPolar[1] & @LF & _
"Theta (Degrees) = " & \$avPolar[2], 10)
Sleep(250)
EndIf
Sleep(10)
WEnd

Func _Quit()
Exit
EndFunc  ;==>_Quit

; ===================================================================================
; Function:  _xy2polar()
; Purpose:  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)
Local \$Pi = 4 * ATan(1), \$PolarAnswer[3], \$r, \$ThetaDeg, \$ThetaRad

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

\$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```

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

#### Share this post

##### Share on other sites

Thanks but i'm not sure what your function does. Does it have to do with the % 360 thing ?

#### Share this post

##### Share on other sites

% means modulo, Mod() in AutoIt.

#### Share this post

##### Share on other sites

% means modulo, Mod() in AutoIt.

Thanks, so it should be this?

`return Mod(((_ATan2(\$deltaX, -1 * \$deltaY) * 180 / \$pi) + 360), 360)`

#### Share this post

##### Share on other sites

yea, that's correct.

#### Share this post

##### Share on other sites

Thanks but i'm not sure what your function does. Does it have to do with the % 360 thing ?

No, that is like _Atan2() plus some more.

... you know how it is with old people

.

eMyvnE

#### Share this post

##### Share on other sites

Here be another, with comments.

```Global Const \$pi = 3.14159265358979

;Note To find the angle form point, (\$iX1, \$iY1) to second point, (\$iX2, \$iY2)
;     find arc tan of (change in vertical distance divided by change in horizontal distance).
;    We have :-
;Local \$deltaY = \$iY2 - \$iY1 ; Change in vertical distance
;local \$deltaX = \$iX2 - \$iX1 ; Change in horizontal distance
;  Then use
;Local \$angle = DirectionAngle(\$deltaX, \$deltaY)  ; Where \$angle variable will contain the angle in degrees from
;           point, (\$iX1, \$iY1) to second point, (\$iX2, \$iY2) off the X-axis.(X-axis positive being zero degrees.)

MsgBox(0, "", "Screen Coordinate system Y-Axis Down, X-Axis to Right Positive, " & @CRLF & _
"zero Deg. = X-axis increasing clockwise." & @CRLF & @CRLF & _
StringFormat("     Along X-axis x =  100  Y =    0  gives %.1f Degrees", DirectionAngle(100, 0)) & @CRLF & _
StringFormat("     1st Quadrant x =  100  Y =  100  gives %.1f Degrees", DirectionAngle(100, 100)) & @CRLF & _
StringFormat("     Along Y-axis x =    0  Y =  100  gives %.1f Degrees", DirectionAngle(0, 100)) & @CRLF & _
StringFormat("     2nd Quadrant x = -100  Y =  100  gives %.1f Degrees", DirectionAngle(-100, 100)) & @CRLF & _
StringFormat("Neg. Along X-axis x = -100  Y =    0  gives %.1f Degrees", DirectionAngle(-100, 0)) & @CRLF & _
StringFormat("     3rd Quadrant x = -100  Y = -100  gives %.1f Degrees", DirectionAngle(-100, -100)) & @CRLF & _
StringFormat("Neg. Along Y-axis x =    0  Y = -100  gives %.1f Degrees", DirectionAngle(0, -100)) & @CRLF & _
StringFormat("     4th Quadrant x =  100  Y = -100  gives %.1f Degrees", DirectionAngle(100, -100)) & @CRLF)

; \$deltaX is change in horizontal distance
; \$deltaY is change in vertical distance
; Return angle (degrees)
Func DirectionAngle(\$deltaX, \$deltaY)
Local \$angle = ATan(\$deltaY / \$deltaX)
\$angle += ((\$deltaY <= 0) And (\$deltaX < 0)) * \$pi ; Correction for 3th quadrant
\$angle += ((\$deltaY > 0) And (\$deltaX < 0)) * \$pi  ; Correction for 2nd quadrant
Return Round(Mod(\$angle * 180 / \$pi + 360, 360), 12) ; The +360 avoids negative angles( -45Deg = 315Deg)
EndFunc   ;==>DirectionAngle```

#### Share this post

##### Share on other sites

Here be another, with comments.

```Global Const \$pi = 3.14159265358979

;Note To find the angle form point, (\$iX1, \$iY1) to second point, (\$iX2, \$iY2)
;     find arc tan of (change in vertical distance divided by change in horizontal distance).
;    We have :-
;Local \$deltaY = \$iY2 - \$iY1 ; Change in vertical distance
;local \$deltaX = \$iX2 - \$iX1 ; Change in horizontal distance
;  Then use
;Local \$angle = DirectionAngle(\$deltaX, \$deltaY)  ; Where \$angle variable will contain the angle in degrees from
;           point, (\$iX1, \$iY1) to second point, (\$iX2, \$iY2) off the X-axis.(X-axis positive being zero degrees.)

MsgBox(0, "", "Screen Coordinate system Y-Axis Down, X-Axis to Right Positive, " & @CRLF & _
"zero Deg. = X-axis increasing clockwise." & @CRLF & @CRLF & _
StringFormat("     Along X-axis x =  100  Y =    0  gives %.1f Degrees", DirectionAngle(100, 0)) & @CRLF & _
StringFormat("     1st Quadrant x =  100  Y =  100  gives %.1f Degrees", DirectionAngle(100, 100)) & @CRLF & _
StringFormat("     Along Y-axis x =    0  Y =  100  gives %.1f Degrees", DirectionAngle(0, 100)) & @CRLF & _
StringFormat("     2nd Quadrant x = -100  Y =  100  gives %.1f Degrees", DirectionAngle(-100, 100)) & @CRLF & _
StringFormat("Neg. Along X-axis x = -100  Y =    0  gives %.1f Degrees", DirectionAngle(-100, 0)) & @CRLF & _
StringFormat("     3rd Quadrant x = -100  Y = -100  gives %.1f Degrees", DirectionAngle(-100, -100)) & @CRLF & _
StringFormat("Neg. Along Y-axis x =    0  Y = -100  gives %.1f Degrees", DirectionAngle(0, -100)) & @CRLF & _
StringFormat("     4th Quadrant x =  100  Y = -100  gives %.1f Degrees", DirectionAngle(100, -100)) & @CRLF)

; \$deltaX is change in horizontal distance
; \$deltaY is change in vertical distance
; Return angle (degrees)
Func DirectionAngle(\$deltaX, \$deltaY)
Local \$angle = ATan(\$deltaY / \$deltaX)
\$angle += ((\$deltaY <= 0) And (\$deltaX < 0)) * \$pi ; Correction for 3th quadrant
\$angle += ((\$deltaY > 0) And (\$deltaX < 0)) * \$pi  ; Correction for 2nd quadrant
Return Round(Mod(\$angle * 180 / \$pi + 360, 360), 12) ; The +360 avoids negative angles( -45Deg = 315Deg)
EndFunc   ;==>DirectionAngle```
That one really helped me. Thanks.

My next challenge of my new project is described here: http://www.autoitscript.com/forum/index.php?showtopic=82145

Thanks!

#### Share this post

##### Share on other sites

wrong post sorry

Edited by Hawkysoft

## 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

×

• Wiki

• Back

• Git