Jump to content

Mathematics, coordinates with radius of a circle?


Go to solution Solved by sphexion,

Recommended Posts

Ok, I did this a fue years ago but I've forgot how I achieved it.

Here are old functions used.

Func _GetAngleDegree($X1, $Y1, $X2, $Y2)
    Local Const $nPI = 4 * ATan(1)
    Local $XDIFF, $YDIFF, $TempAngle
    $YDIFF = Abs($Y2 - $Y1)
    If $X1 = $X2 And $Y1 = $Y2 Then Return 0
    If $YDIFF = 0 And $X1 < $X2 Then
        Return 0
    ElseIf $YDIFF = 0 And $X1 > $X2 Then
        Return $nPI
    EndIf
    $XDIFF = Abs($X2 - $X1)
    $TempAngle = ATan($XDIFF / $YDIFF)
    If $Y2 > $Y1 Then $TempAngle = $nPI - $TempAngle
    If $X2 < $X1 Then $TempAngle = -$TempAngle
    $TempAngle = ($nPI / 2) - $TempAngle
    If $TempAngle < 0 Then $TempAngle = ($nPI * 2) + $TempAngle
    Return $TempAngle * 180 / $nPI
EndFunc   ;==>_GetAngleDegree

Func _Angle($tX, $tY, $cX, $cY)
    $mx = $tX - $cX
    $my = $cY - $tY
    $angle = ATan($my / $mx) * $180_div_pi
    If $mx < 0 Then
        $angle = 180 + $angle
    ElseIf $mx >= 0 And $my < 0 Then
        $angle = 360 + $angle
    EndIf
    Return Int($angle)
EndFunc   ;==>_Angle

Func Angle($X1, $Y1, $Ang, $Length)
    Local $Return[3]
    $Return[1] = $X1 + ($Length * Cos($Ang / 180 * 3.14159265358979))
    $Return[2] = $Y1 - ($Length * Sin($Ang / 180 * 3.14159265358979))
    Return $Return
EndFunc   ;==>Angle

The goal!

Identify what the coordinates are for the radius.
 

lets just say we are in a 500px by 500px box
The center is 250x250
0 or 360 degrees or north is Radius=160px straight up the x axis. so 90x250

Now my issue, Lets say there is something showing up at 90 degrees.
How do I calculate what the coordinates are for the variable that would be degrees? Witch in the example is 90. and the cords are unknown.

Edited by sphexion
Link to comment
Share on other sites

Let me try and get this straight. You mean, calculate the intersection of a line from the center of the box reaching out at X degrees angle and one of the edges of the square box? Like...

FDC3JJf.png
?

Doesn't seem to hard with a bit of geometry...

Please confirm that this is what you mean or offer a better example?

Roses are FF0000, violets are 0000FF... All my base are belong to you.

Link to comment
Share on other sites

wow,never done some maths with coding, this may work for the first quadrant properly, for the others my imagination is a bit blurry at this hours

Func _GetIntersect($degrees,$BoxWidth = 300,$BoxHeight = 300) ; [0] = X, [1] = Y
    Local Const $PI = 3.141592653589793, $rads = $degrees*$PI/180
    Local $CircleCenterX = ($BoxWidth * 0.5), $CircleCenterY = ($BoxHeight * 0.5), $coor[2]
    $tan = Tan($rads) * 180 / $PI
    If $degrees < 0 Then $degrees = $degrees * -1
    Switch $degrees
        Case ($degrees/90) < 45
            $coor[0] = $CircleCenterX
            $coor[1] = $CircleCenterX * $tan
        Case ($degrees/90) >= 45
            $coor[0] = $CircleCenterY / $tan
            $coor[1] = $CircleCenterY
        Case Else ; Does this even will happen?
            $coor[0] = "Kim's"
            $coor[1] = "Butt" 
    EndSwitch
    Return $coor
EndFunc
EDIT: I'm trying to solve the 90deg issue, but I'm failing to found a function for the quadrant number (which is stupid, looks like a easy task, but I can't sleep without solving this lol)

 

Func _GetIntersect($degrees,$BoxWidth = 300,$BoxHeight = 300) ; [0] = X, [1] = Y
    Local Const $PI = 3.141592653589793, $rads = $degrees*$PI/180
    Local $CircleCenterX = ($BoxWidth * 0.5), $CircleCenterY = ($BoxHeight * 0.5), $inDeg = $degrees, $coor[2], $chang = 1
    If IsInt($degrees/90) Then
        $tan = 0
    Else
        $tan = Tan($rads)
    EndIf
    If $degrees < 0 Then
        $degrees = $degrees * -1
        $chang = -1
    EndIf
    Switch $degrees
        Case ($degrees/90) < 45 And $tan <> 0
            $coor[0] = $CircleCenterX * $chang
            $coor[1] = $CircleCenterX * $tan
        Case ($degrees/90) >= 45 And $tan <> 0
            $coor[0] = $CircleCenterY / $tan
            $coor[1] = $CircleCenterY * $chang
        Case $tan = 0
            If $chang = 1 Then ; Positive angle
                ; #### AND I'M STUCK HERE
                $port = $degrees / 90
                Select
                    Case Mod($degrees,
                    Case
                $coor[0] = 
                $coor[1] = 
            Else
                $coor[0] = 
                $coor[1] =
            EndIf
        Case Else ; Does this even will happen?
            $coor[0] = "Kim's"
            $coor[1] = "Butt" 
    EndSwitch
    Return $coor
EndFunc

EDIT2: Negative angles need to be looked at...just made for the positive ones in case of Mod(degrees,90) = 0

Func _GetIntersect($degrees,$BoxWidth = 300,$BoxHeight = 300) ; [0] = X, [1] = Y
    Local Const $PI = 3.141592653589793, $rads = $degrees*$PI/180
    Local $CircleCenterX = ($BoxWidth * 0.5), $CircleCenterY = ($BoxHeight * 0.5), $inDeg = $degrees, $coor[2], $chang = 1
    If IsInt($degrees/90) Then
        $tan = 0
    Else
        $tan = Tan($rads)
    EndIf
    If $degrees < 0 Then
        $degrees = $degrees * -1
        $chang = -1
    EndIf
    Switch $degrees
        Case ($degrees/90) < 45 And $tan <> 0
            $coor[0] = $CircleCenterX * $chang
            $coor[1] = $CircleCenterX * $tan
        Case ($degrees/90) >= 45 And $tan <> 0
            $coor[0] = $CircleCenterY / $tan
            $coor[1] = $CircleCenterY * $chang
        Case $tan = 0
            If $chang = 1 Then ; Positive angle
                $quad = Mod($degrees/90,4)
                Switch $quad
                    Case 0
                        $coor[0] = $CircleCenterX
                        $coor[1] = 0
                    Case 1
                        $coor[0] = 0
                        $coor[1] = $CircleCenterY
                    Case 2
                        $coor[0] = - $CircleCenterX
                        $coor[1] = 0
                    Case 3
                        $coor[0] = 0
                        $coor[1] = - $CircleCenterY
                EndSwitch
            Else
                $coor[0] = 
                $coor[1] = 
            EndIf
        Case Else ; Does this even will happen?
            $coor[0] = "Kim's"
            $coor[1] = "Butt" 
    EndSwitch
    Return $coor
EndFunc
My sketch pad

0*90 ->x=W/2 y = 0
1*90 ->x=0  y=H/2
2*90 ->x=-W/2 y = 0
3*90 ->x = 0 y =-H/2

4*90 ->x = W/2 y = 0

EDIT3: This might work for all, not sure if I got straight the negative quadrants

Func _GetIntersect($degrees,$BoxWidth = 300,$BoxHeight = 300) ; [0] = X, [1] = Y
    Local Const $PI = 3.141592653589793, $rads = $degrees*$PI/180
    Local $CircleCenterX = ($BoxWidth * 0.5), $CircleCenterY = ($BoxHeight * 0.5), $inDeg = $degrees, $coor[2], $chang = 1
    If IsInt($degrees/90) Then
        $tan = 0
    Else
        $tan = Tan($rads)
    EndIf
    If $degrees < 0 Then
        $degrees = $degrees * -1
        $chang = -1
    EndIf
    Switch $degrees
        Case ($degrees/90) < 45 And $tan <> 0
            $coor[0] = $CircleCenterX * $chang
            $coor[1] = $CircleCenterX * $tan
        Case ($degrees/90) >= 45 And $tan <> 0
            $coor[0] = $CircleCenterY / $tan
            $coor[1] = $CircleCenterY * $chang
        Case $tan = 0
            If $chang = 1 Then ; Positive angle
                $quad = Mod($degrees/90,4)
                Switch $quad
                    Case 0
                        $coor[0] = $CircleCenterX
                        $coor[1] = 0
                    Case 1
                        $coor[0] = 0
                        $coor[1] = $CircleCenterY
                    Case 2
                        $coor[0] = - $CircleCenterX
                        $coor[1] = 0
                    Case 3
                        $coor[0] = 0
                        $coor[1] = - $CircleCenterY
                EndSwitch
            Else
                $quad = Mod(-$degrees/90,4)+4
                Switch $quad
                    Case 1
                        $coor[0] = 0
                        $coor[1] = $CircleCenterY
                    Case 2
                        $coor[0] = - $CircleCenterX
                        $coor[1] = 0
                    Case 3
                        $coor[0] = 0
                        $coor[1] = - $CircleCenterY
                    Case 4
                        $coor[0] = $CircleCenterX
                        $coor[1] = 0
                EndSwitch
            EndIf
        Case Else ; Does this even will happen?
            $coor[0] = "Kim's"
            $coor[1] = "Butt" 
    EndSwitch
    Return $coor
EndFunc
EDIT4: Looking good, didn't found any flaw on the EDIT3 code :D

 

Test:

Global $TestAngles[11] = [-450,-360,-270,-180,-90,0,90,180,270,360,450]
For $a = 0 To UBound($TestAngles)-1
    $intersect = _GetIntersect($TestAngles[$a])
    ConsoleWrite("Angle:"&$TestAngles[$a]&@LF&"X:"&$intersect[0]&@LF&"Y:"&$intersect[1]&@LF&"=============="&@LF)
Next
Exit
Output

Angle:-450
X:0
Y:-150
==============
Angle:-360
X:150
Y:0
==============
Angle:-270
X:0
Y:150
==============
Angle:-180
X:-150
Y:0
==============
Angle:-90
X:0
Y:-150
==============
Angle:0
X:150
Y:0
==============
Angle:90
X:0
Y:150
==============
Angle:180
X:-150
Y:0
==============
Angle:270
X:0
Y:-150
==============
Angle:360
X:150
Y:0
==============
Angle:450
X:0
Y:150
==============

BTW, I'm using coordinates relative to circle's center, and the same angle origin as the unit circle.

 

EDIT5: Code with lesser lines (don't know why, but can't make inline if statements work)

Func _GetIntersect($degrees,$BoxWidth = 300,$BoxHeight = 300) ; [0] = X, [1] = Y
    Local Const $PI = 3.141592653589793, $rads = $degrees*$PI/180
    Local $CircleCenterX = ($BoxWidth * 0.5), $CircleCenterY = ($BoxHeight * 0.5), $coor[2], $ChangeSignal= 1, $tan = Tan($rads), $add = 1
    If IsInt($degrees/90) Then $tan = 0
    If $degrees < 0 Then
        $degrees = $degrees * -1
        $ChangeSignal= -1
    EndIf
    Switch $degrees
        Case ($degrees/90) < 45 And $tan <> 0
            $coor[0] = $CircleCenterX * $ChangeSignal
            $coor[1] = $CircleCenterX * $tan
        Case ($degrees/90) >= 45 And $tan <> 0
            $coor[0] = $CircleCenterY / $tan
            $coor[1] = $CircleCenterY * $ChangeSignal
        Case $tan = 0
            If $ChangeSignal = -1 Then $add = 4 ; If the number is negative
            $quad = Mod($degrees/90,4)+$add
            Switch $quad
                Case 4
                    $coor[0] = $CircleCenterX
                    $coor[1] = 0
                Case 1
                    $coor[0] = 0
                    $coor[1] = $CircleCenterY
                Case 2
                    $coor[0] = - $CircleCenterX
                    $coor[1] = 0
                Case 3
                    $coor[0] = 0
                    $coor[1] = - $CircleCenterY
            EndSwitch
    EndSwitch
    Return $coor
EndFunc
Edited by Kyan

Heroes, there is no such thing

One day I'll discover what IE.au3 has of special for so many users using it.
C'mon there's InetRead and WinHTTP, way better
happy.png

Link to comment
Share on other sites

OK, thanks for the support.
I have figured it out with my previous code after a nights rest, it was more simple then I thought.
The  Angle($centerX,$centerY2, $_ang, $radius) is not dependent on the other function.
It will return the coordinates of center axis with designated angel and radius.

Local Const $180_div_pi = 180 / ACos(-1)
$centerX = 19
$centerY = 762
$radius = 160
$targetX =194
$targetY = 922

$_ang = _Angle($targetX,$targetY,$centerX,$centerY) ; test 90deg
$rod = Angle($centerX,$centerY2, $_ang, $radius)

ClipPut($_ang&"o:"&$rod[1] &":"& $rod[2])
ToolTip($_ang&"o:"&$rod[1] &":"& $rod[2])
Sleep(5000)

Func _Angle($tX, $tY, $cX, $cY)
    $mx = $tX - $cX
    $my = $cY - $tY
    $angle = ATan($my / $mx) * $180_div_pi
    If $mx < 0 Then
        $angle = 180 + $angle
    ElseIf $mx >= 0 And $my < 0 Then
        $angle = 360 + $angle
    EndIf
    Return Int($angle)
EndFunc   ;==>_Angle

Func Angle($X1, $Y1, $Ang, $Length)
    Local $Return[3]
    $Return[1] = $X1 + ($Length * Cos($Ang / 180 * 3.14159265358979))
    $Return[2] = $Y1 - ($Length * Sin($Ang / 180 * 3.14159265358979))
    Return $Return
EndFunc   ;==>Angle
Link to comment
Share on other sites

Full test functionality.
 

For $i = 0 To 360 Step +1
$rod = Angle($centerX,$centerY, $i, $radius)
ToolTip($i&"o:"&$rod[1] &":"& $rod[2])
MouseMove($rod[1] , $rod[2],0)
Sleep(10)
Next

+90 degree offset check
 

For $i = 0 To 360 Step +1
$angle = $i + 90
$rod = Angle($centerX,$centerY, $angle, $radius)
ToolTip($angel&"o:"&$rod[1] &":"& $rod[2])
MouseMove($rod[1] , $rod[2],0)
Sleep(10)
Next
Edited by sphexion
Link to comment
Share on other sites

  • Solution

Now I have given birth to the _PixelSearchCircumference()
I wanted to design a way of searching a radar, witch is outer Circumference for targets to keep from poinless searches and useless scans out of the region.
Now it works perfectly. I am sure it can be twked to search inner Circumference  also.

 

Local Const $180_div_pi = 180 / ACos(-1)

$it = _PixelSearchCircumference(194, 762, 160, 0xFA3C32)
MouseMove($it[1],$it[2],0)
Sleep(5000)

Func _Angle($tX, $tY, $cX, $cY)
    $mx = $tX - $cX
    $my = $cY - $tY
    $angle = ATan($my / $mx) * $180_div_pi
    If $mx < 0 Then
        $angle = 180 + $angle
    ElseIf $mx >= 0 And $my < 0 Then
        $angle = 360 + $angle
    EndIf
    Return Int($angle)
EndFunc   ;==>_Angle

Func Angle($X1, $Y1, $Ang, $Length)
    Local $Return[3]
    $Return[1] = $X1 + ($Length * Cos($Ang / 180 * 3.14159265358979))
    $Return[2] = $Y1 - ($Length * Sin($Ang / 180 * 3.14159265358979))
    Return $Return
EndFunc   ;==>Angle

Func _PixelSearchCircumference($centerX, $centerY, $radius, $color)
For $i = 0 To 360 Step +1
$angle = $i + 90
$rod = Angle($centerX,$centerY, $angle, $radius)
$iColor = PixelGetColor($rod[1] , $rod[2])
If $iColor == $color Then
Return $rod
ExitLoop
EndIf
Next
EndFunc
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...