Question about Coord System Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
masheen
Posts: 295
Joined: 06 Dec 2016, 14:10

Question about Coord System

30 Mar 2018, 12:57

Hi I recently asked a question on the forum about how to get value1? https://autohotkey.com/boards/viewtopic.php?f=5&t=46216
Image

Code: Select all

Pi := 4 * Atan(1) ;3.14
maxCam := 4 * Atan(1) * 2 ;6.28
value1 := maxCam / (2 * Pi) * (Atan((Y2 - Y1) / (X2 - X1)) + 3 * Pi / 2) - (X1 < X2 ? -Pi : 0) ;Work is good
I need help again. How to get value1 and value2?
Image

What i do wrong?

Code: Select all

value2 := Atan((Y2 - Y1) / (X2 - X1)) / Pi + 0.5
value3 := Atan((Y3 - Y1) / (X3 - X1)) / Pi + 0.5
Or If I imagine that the semicircle are 2 circles that what wrong?
Image

Code: Select all

Pi := 4 * Atan(1) ;3.14
maxCam := 2
value2 := maxCam / (2 * Pi) * (Atan((Y2 - Y1) / (X2 - X1)) + 3 * Pi / 2) - (X1 < X2 ? -2 : 0)
value3 := maxCam / (2 * Pi) * (Atan((Y2 - Y1) / (X2 - X1)) + 3 * Pi / 2) - (X1 > X2 ? -2 : 0)
Plz help
Last edited by masheen on 30 Mar 2018, 14:59, edited 1 time in total.
wolf_II
Posts: 2688
Joined: 08 Feb 2015, 20:55

Re: Question about Coord System

30 Mar 2018, 14:59

The problem lies within using the ATan() function, It does not do what you hope it to do.
ATan() gives back value between -Pi/2 and +Pi/2. You need a function like ATan2() to get back a range from -Pi to +Pi.
Check Wikipedia.

Code: Select all

ATan2(y, x) { ; ATan() with correct sign
    static PI := 4 * ATan(1)

    If (x > 0)
        Return, ATan(y/x)
    If (x < 0)
        Return, ATan(y/x) + (y >= 0 ? PI : -PI)
    ; else x = 0
    Return, (y > 0) ? Math.PI/2 : (y < 0) ? -PI/2 : ""
}
User avatar
masheen
Posts: 295
Joined: 06 Dec 2016, 14:10

Re: Question about Coord System

30 Mar 2018, 15:11

wolf_II wrote:The problem lies within using the ATan() function, It does not do what you hope it to do.
ATan() gives back value between -Pi/2 and +Pi/2. You need a function like ATan2() to get back a range from -Pi to +Pi.
Check Wikipedia.

Code: Select all

ATan2(y, x) { ; ATan() with correct sign
    static PI := 4 * ATan(1)

    If (x > 0)
        Return, ATan(y/x)
    If (x < 0)
        Return, ATan(y/x) + (y >= 0 ? PI : -PI)
    ; else x = 0
    Return, (y > 0) ? Math.PI/2 : (y < 0) ? -PI/2 : ""
}
Can tell which of these functions does not work correctly?

Code: Select all

Pi := 4 * Atan(1) ;3.14
maxCam := 4 * Atan(1) * 2 ;6.28
value1 := maxCam / (2 * Pi) * (Atan((Y2 - Y1) / (X2 - X1)) + 3 * Pi / 2) - (X1 < X2 ? -Pi : 0) ;Work is good

Code: Select all

value2 := Atan((Y2 - Y1) / (X2 - X1)) / Pi + 0.5
value3 := Atan((Y3 - Y1) / (X3 - X1)) / Pi + 0.5

Code: Select all

Pi := 4 * Atan(1) ;3.14
maxCam := 2
value2 := maxCam / (2 * Pi) * (Atan((Y2 - Y1) / (X2 - X1)) + 3 * Pi / 2) - (X1 < X2 ? -2 : 0)
value3 := maxCam / (2 * Pi) * (Atan((Y2 - Y1) / (X2 - X1)) + 3 * Pi / 2) - (X1 > X2 ? -2 : 0)
wolf_II
Posts: 2688
Joined: 08 Feb 2015, 20:55

Re: Question about Coord System

30 Mar 2018, 15:43

Look for this pattern: ATan(dy / dx) and replace with this: ATan2(dy, dx).
User avatar
masheen
Posts: 295
Joined: 06 Dec 2016, 14:10

Re: Question about Coord System

30 Mar 2018, 16:40

wolf_II wrote:Look for this pattern: ATan(dy / dx) and replace with this: ATan2(dy, dx).
Same result. If I misunderstood what is dx dy then I'm sorry, geometry I have trouble.

Code: Select all

dy := 5.2
dx := 7.9

MsgBox % ATan(dy / dx) ;0.582138 
MsgBox % ATan2(dy, dx) ;0.582138

ATan2(y, x) { ; ATan() with correct sign
    static PI := 4 * ATan(1)

    If (x > 0)
        Return, ATan(y/x)
    If (x < 0)
        Return, ATan(y/x) + (y >= 0 ? PI : -PI)
    ; else x = 0
    Return, (y > 0) ? Math.PI/2 : (y < 0) ? -PI/2 : "" ;<--- is this right? -Pi/2 and +Pi/2. >> -Pi to +Pi.
}
wolf_II
Posts: 2688
Joined: 08 Feb 2015, 20:55

Re: Question about Coord System

30 Mar 2018, 16:51

Try with numbers from other quadrant also, Like dx=-3 and dy=+3 for example.
Compare two points which both lie on the diagonal with slope of -1: (Angle in degrees = -45 or 135)
ATan() goes between +90 and -90, and you seem to prefer 135 as result if possible, as far as I understand you.

Code: Select all

; point in pos y, neg x quadrant
dy := 3
dx := -3
MsgBox % Deg(ATan(dy / dx)) ; -45 math correct, but not interested
MsgBox % Deg(ATan2(dy, dx)) ; 135 want this instead

; point in neg y, pos x quadrant
dy := -3
dx := 3
MsgBox % Deg(ATan(dy / dx)) ; -45 same value, math also correct
MsgBox % Deg(ATan2(dy, dx)) ; -45


;-------------------------------------------------------------------------------
class Math { ; constant Math.PI
    static PI := 4 * ATan(1)
}
Rad(Angle) { ; convert angle in degrees => angle in radians
    Return, Angle * Math.PI / 180
}
Deg(Angle) { ; convert angle in radians => angle in degrees
    Return, Angle * 180 / Math.PI
}
ATan2(y, x) { ; ATan() with full range
    If (x > 0)
        Return, ATan(y/x)
    If (x < 0)
        Return, ATan(y/x) + (y >= 0 ? Math.PI : -Math.PI)
    ; else x = 0
    Return, (y > 0) ? Math.PI/2 : (y < 0) ? -Math.PI/2 : ""
}
User avatar
masheen
Posts: 295
Joined: 06 Dec 2016, 14:10

Re: Question about Coord System

31 Mar 2018, 01:48

wolf_II wrote:Try with numbers from other quadrant also, Like dx=-3 and dy=+3 for example.
Compare two points which both lie on the diagonal with slope of -1: (Angle in degrees = -45 or 135)
ATan() goes between +90 and -90, and you seem to prefer 135 as result if possible, as far as I understand you.

Code: Select all

; point in pos y, neg x quadrant
dy := 3
dx := -3
MsgBox % Deg(ATan(dy / dx)) ; -45 math correct, but not interested
MsgBox % Deg(ATan2(dy, dx)) ; 135 want this instead

; point in neg y, pos x quadrant
dy := -3
dx := 3
MsgBox % Deg(ATan(dy / dx)) ; -45 same value, math also correct
MsgBox % Deg(ATan2(dy, dx)) ; -45


;-------------------------------------------------------------------------------
class Math { ; constant Math.PI
    static PI := 4 * ATan(1)
}
Rad(Angle) { ; convert angle in degrees => angle in radians
    Return, Angle * Math.PI / 180
}
Deg(Angle) { ; convert angle in radians => angle in degrees
    Return, Angle * 180 / Math.PI
}
ATan2(y, x) { ; ATan() with full range
    If (x > 0)
        Return, ATan(y/x)
    If (x < 0)
        Return, ATan(y/x) + (y >= 0 ? Math.PI : -Math.PI)
    ; else x = 0
    Return, (y > 0) ? Math.PI/2 : (y < 0) ? -Math.PI/2 : ""
}
Thanks. Can you help me with the formula? I need to make my programm know what direction (left-right) to turn the camera on the nearest road. Known data there is X1Y1 X2Y2, vCurPosCam(in radian), vNeedlePosCam(in radian), vMaxPosCam(in radian). The camera rotates anti-clockwise. How to show my programm that need to turn right as it's the fastest way? In this case, it should turn right.
Image
wolf_II
Posts: 2688
Joined: 08 Feb 2015, 20:55

Re: Question about Coord System

31 Mar 2018, 02:51

Please check the following angles: 0 degrees (= 0 radians) and 90 degrees (= Pi/2 radians).
You will find that points on the x-axis like {x: 5, y: 0) have an angle of 0 degrees.
You will also find that points on the y-axis like {x: 0, y: 5} or {x: 0, y: -5} have an angle of +/-90 degrees.

Next: adjust your diagram to use the correct values. I see 0 degrees is pointing down in your diagram.
If you want to use the angle you get from Atan() or ATan2(), you're better off with zero degrees pointing right and 90 degrees pointing up.
And every angle from 0 degrees to 90 degrees falls into the first quadrant (upper-right = positve x, positive y)
User avatar
masheen
Posts: 295
Joined: 06 Dec 2016, 14:10

Re: Question about Coord System

31 Mar 2018, 03:38

wolf_II wrote:Please check the following angles: 0 degrees (= 0 radians) and 90 degrees (= Pi/2 radians).
You will find that points on the x-axis like {x: 5, y: 0) have an angle of 0 degrees.
You will also find that points on the y-axis like {x: 0, y: 5} or {x: 0, y: -5} have an angle of +/-90 degrees.

Next: adjust your diagram to use the correct values. I see 0 degrees is pointing down in your diagram.
If you want to use the angle you get from Atan() or ATan2(), you're better off with zero degrees pointing right and 90 degrees pointing up.
And every angle from 0 degrees to 90 degrees falls into the first quadrant (upper-right = positve x, positive y)
Thanks for reply. This is very difficult for me, my knowledge of geometry is almost at zero.
I understan that I need to get a positive or negative sign, but did not understand how.
If you can give me a formula I will be happy. I google it and found this but what is x3 and y3?

Code: Select all

Alpha := ((x2-x1) * (y3-y1) - (y2-y1) * (x3-x1)) / 2
Also find it. Don't work.

Code: Select all

Alpha := arctan(((x1 * y2) - (y1 * x2)) / ((x1 * x2) + (y1 * y2))) ; arctan = Atan
wolf_II
Posts: 2688
Joined: 08 Feb 2015, 20:55

Re: Question about Coord System  Topic is solved

31 Mar 2018, 04:15

After you adjusted your diagram, your question will be as simple as:
To rotate from currentAngle to desiredAngle, which way is shorter - turning right (clockwise = math neg) or turning left (from 0 towards 90, anti-clockwise, math pos)?
Answer:

Code: Select all

; use standard math
Direction := +1                                 ; math pos = anti-clockwise
AngleBetween := desiredAngle - currentAngle     ; this is our "candidate"

If Abs(AngleBetween) > Rad(180) { ; turning more than 180? is too far!
    AngleBetween *= -1
    Direction *= -1
}

If (Direction = 1)
    MsgBox, anti-clockwise is shorter %AngleBetween%
Else If (Direction = -1)
    MsgBox, clockwise is shorter %AngleBetween%
Else
    MsgBox, no need to turn
This should be close, I hope. :?:
User avatar
masheen
Posts: 295
Joined: 06 Dec 2016, 14:10

Re: Question about Coord System

31 Mar 2018, 08:06

wolf_II wrote:After you adjusted your diagram, your question will be as simple as:
To rotate from currentAngle to desiredAngle, which way is shorter - turning right (clockwise = math neg) or turning left (from 0 towards 90, anti-clockwise, math pos)?
Answer:

Code: Select all

; use standard math
Direction := +1                                 ; math pos = anti-clockwise
AngleBetween := desiredAngle - currentAngle     ; this is our "candidate"

If Abs(AngleBetween) > Rad(180) { ; turning more than 180? is too far!
    AngleBetween *= -1
    Direction *= -1
}

If (Direction = 1)
    MsgBox, anti-clockwise is shorter %AngleBetween%
Else If (Direction = -1)
    MsgBox, clockwise is shorter %AngleBetween%
Else
    MsgBox, no need to turn
This should be close, I hope. :?:

Thanks to this post was very useful, I disassembled your examples, and understood how my examples work.
And now I know how all these functions work. But I'm going to do everything on my own, and I'll check it, then write here what I get.
User avatar
masheen
Posts: 295
Joined: 06 Dec 2016, 14:10

Re: Question about Coord System

01 Apr 2018, 00:39

if I'd seen this before and I'd have understood. This is easy, like 2+2
Image
User avatar
masheen
Posts: 295
Joined: 06 Dec 2016, 14:10

Re: Question about Coord System

04 Apr 2018, 06:11

wolf_II wrote:After you adjuste...
It's a little different than what you showed, but thank you so much for the idea. Need AngleBetween * Direction.
Image

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Giresharu, Joey5, supplementfacts and 131 guests