Sign in to follow this  
Followers 0
cili1

X/Y (Calculating angle, problem)

3 posts in this topic

Hello!

I'm making a program that calculates the angle to a position on the X/Y axis.

The problem is that as soon as I input a negative position (for example. -2, -2) it resets at 180° and "starts all over again".

I want to be able to get the angles above 180° as I illustrate in the picture below.

The blue text shows what values it shows at the moment.

Green text is what I want it to show.

Posted Image

#include <GUIConstants.au3>
#include <Math.au3>

$Form1 = GUICreate("Angle?", 180, 60, 500, 400)
GUISetState(@SW_SHOW)
GUICtrlCreateLabel("Y", 5, 5, 15, 20)
GUICtrlCreateLabel("X", 5, 30, 15, 20)
$GUI_a = GUICtrlCreateInput("", 20, 5, 150, 20)
$GUI_c = GUICtrlCreateInput("", 20, 30, 150, 20)

HotKeySet("{F1}", "Calc")
HotKeySet("{ESC}", "Terminate")

While 1
WEnd

Func Calc()
    $a = GUICtrlRead($GUI_a)
    $c = GUICtrlRead($GUI_c)

    $b = Sqrt($a*$a+$c*$c)
    
    $calc1 = $b*$b + $c*$c
    $calc2 = -2*$b*$c
    $calc3 = $a*$a - $calc1
    $calc4 = $calc3/$calc2
    
    $cos = ACos($calc4)
    $test = Round(_Degree($cos), 2)
    MsgBox(0, "", $test & "%")
EndFunc

Func Terminate()
    Exit 0
EndFunc

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Returns the angle relative the center of your screen.

MsgBox(0, '', _Angle(@DesktopWidth / 2 + 100, @DesktopHeight / 2 + 100))

Func _Angle($X, $Y)

    Local $CX = @DesktopWidth / 2, $CY = @DesktopHeight / 2

    If ($CX = $X) And ($CY = $Y) Then
        Return SetError(1, 0, -1)
    EndIf

    Local $Grad = ($Y <= $CY) * 180 - ATan(($CX - $X) / ($CY - $Y)) * 180 / 3.1415926535897932384626 + 90

    If $Grad = 360 Then
        $Grad = 0
    EndIf
    Return $Grad
EndFunc   ;==>_Angle
Edited by Yashied

Share this post


Link to post
Share on other sites

Returns the angle relative the center of your screen.

Thank you!

Problem solved.

Share this post


Link to post
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
Sign in to follow this  
Followers 0