# Get angle with cos and sin

## Recommended Posts

Hello ! I made a function that draw a circle with the mouse. In this function, I have an argument that is the angle where the circle is starting drawn.

With another function, I click on the screen a first time to point where the circle will start to be drawn, and a second time to point the center of my circle.

My problem is I don't manage to calculate the angle with this two points.

Here is my code :

```Func ask_circle()
\$angle = InputBox("Angle", "Please type in the angle in degrees you want it draws (360 is a full rotation).Click on OK and then, click one time on your screen for where the circle will start to be drawn, and a second time for the center.", 90)
If @error = 1 Then
MsgBox(64, "Cancelled", "Action cancelled")
Exit
EndIf
Sleep(300)
While 1
If _IsPressed("01") Then
\$start_pos = MouseGetPos()
ExitLoop
EndIf
WEnd
Sleep(300)
While 1
If _IsPressed("01") Then
\$ctr_pos = MouseGetPos()
ExitLoop
EndIf
WEnd
\$diam = Sqrt( ( \$ctr_pos[1] - \$start_pos[1] ) * ( \$ctr_pos[1] - \$start_pos[1] ) +  ( \$ctr_pos[0] - \$start_pos[0] ) * ( \$ctr_pos[0] - \$start_pos[0] ) )
\$sinus = ( \$start_pos[1] - \$ctr_pos[1] ) / \$diam
\$cosinus = ( \$start_pos[0] - \$ctr_pos[0] ) / \$diam
\$angle_init = _Degree(ACos(\$cosinus))
;~  If \$cosinus >= 0 Then
;~      If \$sinus >= 0 Then
;~          \$angle_init = \$angle_init
;~      Else
;~          \$angle_init = 270 + \$angle_init
;~      EndIf
;~  Else
;~      If \$sinus >= 0 Then
;~          \$angle_init = 270 - \$angle_init
;~      Else
;~          \$angle_init = 180 - \$angle_init
;~      EndIf
;~  EndIf
Sleep(300)
Circle(\$ctr_pos[0], \$ctr_pos[1], \$diam, \$angle_init, \$angle)
EndFunc```

\$angle_init should be the angle between the horizontal line of the center and the line formed with the two points (center, and starting point)

\$ctr_pos is [x,y] coords of the center

\$start_pos is [x,y] coords of the starting point

\$diam is the diameter

This code :

```;~  If \$cosinus >= 0 Then
;~      If \$sinus >= 0 Then
;~          \$angle_init = \$angle_init
;~      Else
;~          \$angle_init = 270 + \$angle_init
;~      EndIf
;~  Else
;~      If \$sinus >= 0 Then
;~          \$angle_init = 270 - \$angle_init
;~      Else
;~          \$angle_init = 180 - \$angle_init
;~      EndIf
;~  EndIf```

is the way I wanted to do calculate the angle, but it does not work.

Thank you, cya

Edited by twixman

intrepid

##### Share on other sites

I made many test that I found logical and I had always problem in my tests....

So I checked everything and I found the problem came from the function _Degree() that sometime returned nothing

My new code is :

```\$diam = Sqrt( ( \$ctr_pos[1] - \$start_pos[1] ) * ( \$ctr_pos[1] - \$start_pos[1] ) +  ( \$ctr_pos[0] - \$start_pos[0] ) * ( \$ctr_pos[0] - \$start_pos[0] ) )
\$angle_init = ACos((\$start_pos[0] - \$ctr_pos[0]) / \$diam) * \$radtodeg
If \$start_pos[1] > \$ctr_pos[1] Then \$angle_init = - \$angle_init```

intrepid

## Create an account

Register a new account

×

• Wiki

• Back

• #### Beta

• Git
• FAQ
• Our Picks
×
• Create New...