# Need help with some coordinates

## Recommended Posts

Lets say that i have these coordinates (x, y): 1163, 340 and 1161, 364 and i need to calculate which one is closer to my cursor inside certain range.

##### Share on other sites

Lets say that i have these coordinates (x, y): 1163, 340 and 1161, 364 and i need to calculate which one is closer to my cursor inside certain range.

Oh wow you might of actually solved a problem I was having, I was thinking "How in the hell do I stop this from happening", then a couple of hours later I found your thread and it's perfect for what I need to do! I really hope someone knows how to do this.

##### Share on other sites

pixel functions... get the x & y... then get mousepos and then subtract each by the mouse pos... and the smaller the # the close it is..?

##### Share on other sites

Oh wow you might of actually solved a problem I was having, I was thinking "How in the hell do I stop this from happening", then a couple of hours later I found your thread and it's perfect for what I need to do! I really hope someone knows how to do this.

Im glad that you solved your problem. And i hope that i will solve mine.

pixel functions... get the x & y... then get mousepos and then subtract each by the mouse pos... and the smaller the # the close it is..?

I'll try that..

Edited by tpt

##### Share on other sites

Lets say that i have these coordinates (x, y): 1163, 340 and 1161, 364 and i need to calculate which one is closer to my cursor inside certain range.

If your into Discrete mathematics use Dijkstra's algorithm. Althought there are more effective algorithms this is my favorite. Otherwise there is a simple but stupid way to do it:

```Dim \$Coords[2], \$Coords0[2], \$X[2], \$Y[2]
\$Coords[0] = 1163
\$Coords[1] = 340
\$Coords0[0] = 1161
\$Coords0[1] = 364

\$Pos = MouseGetPos()

\$X[0] = Sqrt(\$Coords[0]^2) -  Sqrt(\$Pos[0]^2)
\$X[1] = Sqrt(\$Coords0[0]^2) -  Sqrt(\$Pos[0]^2)

\$Y[0] = Sqrt(\$Coords[1]^2) -  Sqrt(\$Pos[1]^2)
\$Y[1] = Sqrt(\$Coords0[1]^2) -  Sqrt(\$Pos[1]^2)

If (\$X[0] + \$Y[0]) < (\$X[1] + \$Y[1]) Then
MsgBox( 0, "", "Dot 1 is closer" )
Else
MsgBox( 0, "", "Dot 2 is closer" )
EndIf```
Edited by Qousio

##### Share on other sites

You need to calculate the distance between the points. Look here: http://www.autoitscript.com/forum/index.php?showtopic=93417

```Func Pixel_Distance(\$x1, \$y1, \$x2, \$y2) ;Pythagoras theorem for 2D
Local \$a, \$b, \$c
If \$x2 = \$x1 And \$y2 = \$y1 Then
Return 0
Else
\$a = \$y2 - \$y1
\$b = \$x2 - \$x1
\$c = Sqrt(\$a * \$a + \$b * \$B)
Return \$c
EndIf
EndFunc   ;==>Pixel_Distance```

UEZ

Edited by UEZ

Please don't send me any personal message and ask for support! I will not reply!

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

##### Share on other sites

If your into Discrete mathematics use Dijkstra's algorithm. Althought there are more effective algorithms this is my favorite. Otherwise there is a simple but stupid way to do it:

```Dim \$Coords[2], \$Coords0[2], \$X[2], \$Y[2]
\$Coords[0] = 1163
\$Coords[1] = 340
\$Coords0[0] = 1161
\$Coords0[1] = 364

\$Pos = MouseGetPos()

\$X[0] = Sqrt(\$Coords[0]^2) -  Sqrt(\$Pos[0]^2)
\$X[1] = Sqrt(\$Coords0[0]^2) -  Sqrt(\$Pos[0]^2)

\$Y[0] = Sqrt(\$Coords[1]^2) -  Sqrt(\$Pos[1]^2)
\$Y[1] = Sqrt(\$Coords0[1]^2) -  Sqrt(\$Pos[1]^2)

If (\$X[0] + \$Y[0]) < (\$X[1] + \$Y[1]) Then
MsgBox( 0, "", "Dot 1 is closer" )
Else
MsgBox( 0, "", "Dot 2 is closer" )
EndIf```
It returns dot 1 always, i dont know why.

You need to calculate the distance between the points. Look here: http://www.autoitscript.com/forum/index.php?showtopic=93417

```Func Pixel_Distance(\$x1, \$y1, \$x2, \$y2) ;Pythagoras theorem for 2D
Local \$a, \$b, \$c
If \$x2 = \$x1 And \$y2 = \$y1 Then
Return 0
Else
\$a = \$y2 - \$y1
\$b = \$x2 - \$x1
\$c = Sqrt(\$a * \$a + \$b * \$B)
Return \$c
EndIf
EndFunc   ;==>Pixel_Distance```

UEZ

I don't understand that code. Edited by tpt

##### Share on other sites

It returns dot 1 always, i dont know why.

I don't understand that code.

If you decrease the amount of dot2 it will return dot2... Right now dot1 is closer to the mouse then dot2

For example, this should be simple and even you can understand:

\$Coords[0] = 1

\$Coords[1] = 1

\$Coords0[0] = 2

\$Coords0[1] = 2

Dot 1 will be closer.

\$Coords[0] = 1

\$Coords[1] = 4

\$Coords0[0] = 2

\$Coords0[1] = 2

Dot 2 will be closer.

Simple math =P

Edited by Qousio

##### Share on other sites

It returns dot 1 always, i dont know why.

I don't understand that code.

Look here for an explanation! -> http://gwycon.com/calculating-the-distance...een-two-pixels/

UEZ

Please don't send me any personal message and ask for support! I will not reply!

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

##### Share on other sites

No need to give links really, its simple as one two three.

If \$A is the mouse coords and \$B, \$C are the two dots. Then

The length from \$A to \$B is АВ=sqrt((x2-x1)^2, (y2-y1)^2 , (z2-z1)^2) ; Well in our case its 2d.

x2 is the endpoint, x1 is the startpoint (MousePos)

Edited by Qousio

##### Share on other sites

its not

simple as one two three.

xD its a math forumla lol but i guess if you've used it before then i would be simple

##### Share on other sites

its not

xD its a math forumla lol but i guess if you've used it before then i would be simple

Im far more confused than i were before..

##### Share on other sites

It returns dot 1 always, i dont know why.

I don't understand that code.

Please don't use Qousio's code from the earlier post, it's complete gibberish.

The code you don't understand, ie the code Uez posted, is what you need. If you have a right angled triangle then lets say that the length of the sloping line is H, and the length of the other two sides are A and O. then the relationship is

H*H = A*A + O*O

which is nice and simple.

If you draw a straight line from the cursor at x,y to a point at D,E, and draw a vertical line through the point and a horizontal line through the cursor position you get a right-angled triangle.

The sloping line is H which we want to find.

The horizontal side length is A= x - D

The vertical side is O = y - E

so we can say

H*H = A*A + O*O

or

H = Sqrt(A*A + O*O)

Of course, if x = D and y = E then the result is 0. That's all Uez's function does.

Since you are only interested in which point is nearer, there is no need to find the square root because if the square root is smalle then so is the square, so you could save time by ignoring the square root and work out A*A + O*O for each point.

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

##### Share on other sites

Please don't use Qousio's code from the earlier post, it's complete gibberish.

The code you don't understand, ie the code Uez posted, is what you need. If you have a right angled triangle then lets say that the length of the sloping line is H, and the length of the other two sides are A and O. then the relationship is

H*H = A*A + O*O

which is nice and simple.

If you draw a straight line from the cursor at x,y to a point at D,E, and draw a vertical line through the point and a horizontal line through the cursor position you get a right-angled triangle.

The sloping line is H which we want to find.

The horizontal side length is A= x - D

The vertical side is O = y - E

so we can say

H*H = A*A + O*O

or

H = Sqrt(A*A + O*O)

Of course, if x = D and y = E then the result is 0. That's all Uez's function does.

Since you are only interested in which point is nearer, there is no need to find the square root because if the square root is smalle then so is the square, so you could save time by ignoring the square root and work out A*A + O*O for each point.

How is it gibberish, it works absoloutely fine for me.

I don't know where you study, but we had extensive analytic geometry and integral geometry in my university. A task like this is what 8 year old kids solve at school.

What he is doing is a scalar multiplication in decart coordinates.

0,0 top left screen is the start of the coordinates.

He is trying to find the length between dot A and B, or in other words the length of the vector a.

B-A = Vector a.

a = xi + yj + zk

sqrt(a,a) = |a| = sqrt(x^2+y^2+z^2)

Or to just calculate the distance (which is the same thing)

d=|AB|=sqrt((x2-x1)^2+(y2-y1)^2+(z2-z1)^2)

This can also be done by finding the projection of one point to the other.

Pr(a)B=(a,\|a|

Theres more ways to do this. But seriously, I am very disapointed you tried to argue with me. This math is for 8 year old children in my country.

The best way to prove this method works, even YOU will understand.

(x1i+y1j+z1k, x2i+y2j+z2k) = x1x2+y1y2+z1z2.

This is valid, because; (i,j) = (i,k) = (j,k) = 0; (i,i)=(j,j)=(k,k)=1

You can also find the length outside of decart coordinates, and even when you don't know where point A and point B is.

Edited by Qousio

##### Share on other sites

How is it gibberish, it works absoloutely fine for me.

I don't know where you study, but we had extensive analytic geometry and integral geometry in my university. A task like this is what 8 year old kids solve at school.

What he is doing is a scalar multiplication in decart coordinates.

0,0 top left screen is the start of the coordinates.

He is trying to find the length between dot A and B, or in other words the length of the vector a.

B-A = Vector a.

a = xi + yj + zkI think you meant

sqrt(a,a) = |a| = sqrt(x^2+y^2+z^2)

Or to just calculate the distance (which is the same thing)

d=|AB|=sqrt((x2-x1)^2+(y2-y1)^2+(z2-z1)^2)

This can also be done by finding the projection of one point to the other.

Pr(a)B=(a, \|a|

Theres more ways to do this. But seriously, I am very disapointed you tried to argue with me. This math is for 8 year old children in my country.

The best way to prove this method works, even YOU will understand.

(x1i+y1j+z1k, x2i+y2j+z2k) = x1x2+y1y2+z1z2.

This is valid, because; (i,j) = (i,k) = (j,k) = 0; (i,i)=(j,j)=(k,k)=1

This is what you wrote

Dim \$Coords[2], \$Coords0[2], \$X[2], \$Y[2]

\$Coords[0] = 1163

\$Coords[1] = 340

\$Coords0[0] = 1161

\$Coords0[1] = 364

\$Pos = MouseGetPos()

\$X[0] = Sqrt(\$Coords[0]^2) - Sqrt(\$Pos[0]^2);This is \$X[0] = +/-\$Coords[0] +/- \$Pos[0]

\$X[1] = Sqrt(\$Coords0[0]^2) - Sqrt(\$Pos[0]^2);likewise

\$Y[0] = Sqrt(\$Coords[1]^2) - Sqrt(\$Pos[1]^2);likewise

\$Y[1] = Sqrt(\$Coords0[1]^2) - Sqrt(\$Pos[1]^2);likewise

If (\$X[0] + \$Y[0]) < (\$X[1] + \$Y[1]) Then

MsgBox( 0, "", "Dot 1 is closer" )

Else

MsgBox( 0, "", "Dot 2 is closer" )

EndIf

Suppose the cursor is at 1,1

suppose one dot is at 1,1, and the other dot is at -1,-1. Suppose that when we use your code we take the positive value of the square root. The dot at -1,-1 is 8^0.5 away from the cursor and the one at the cursor is 0 distance away but your formula says they are the same distance away.

If the dots are the same distance away then you say "Dot 2 is closer". Of course I'm being picky now but all that university rant and the 3-D calculations was over the top IMO. What matters to me is not if you went to university but if you are correct.

You can also find the length outside of decart coordinates, and even when you don't know where point A and point B is.

Oh, if you don't know where something is you can still find out how far away it is. Yes, that does make some sense; I don't know where you are but I know it's not on planet Earth.

I can't take any more.

Edited by martin

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

##### Share on other sites

If the dots are the same distance away then you say "Dot 2 is closer". Of course I'm being picky now but all that university rant and the 3-D calculations was over the top IMO. What matters to me is not if you went to university but if you are correct.

Your monitor does NOT have negative coordinates... You can't have a pixel at -1,-1.

The decart coordinate start will not be your mouse, it will always be the topleft corner. I guess this is where you got confused

Oh, if you don't know where something is you can still find out how far away it is. Yes, that does make some sense; I don't know where you are but I know it's not on planet Earth.

I can't take any more.

Yes you can, if you are interested I can pm you. Since this is off topic.

Linear algebra and Analytic geometry actually studies how its done. Later there is integral geometrical equatations, but I hardly understand that.

On Topic: @I just rechecked the code I posted here. Yes it doesn't exactly work ;; I messed up the the correct numbers (Coord0, Coords) I guess I didnt correctly edit it yesterday, althought I remember re-editting it because there was a mistake... Gosh I feel stupid now

Anyway, for simplicitys sake:

```Dim \$Coords[2], \$Coords0[2], \$X[2], \$Y[2]
\$Coords[0] = @desktopwidth
\$Coords[1] = 0
\$Coords0[0] = 0
\$Coords0[1] = @desktopheight

\$Pos = MouseGetPos()

\$Distance1=Sqrt((\$Coords[0]-\$Pos[0])^2+(\$Coords[1]-\$Pos[1])^2)
\$Distance2=Sqrt((\$Coords0[0]-\$Pos[0])^2+(\$Coords0[1]-\$Pos[1])^2)

If \$Distance1 < \$Distance2 Then
MsgBox( 0, "", "Dot 1 is closer" )
Else
MsgBox( 0, "", "Dot 2 is closer" )
EndIf```

Top right = dot1

Top left = dot2

I apologise to martin and tpt, I don't know why I didn't post the correctly edited code. Perhaps I was just sleepy. Although it does say "This post has been edited by Qousio: Yesterday, 11:20 PM" Weird :/

##### Share on other sites

Your monitor does NOT have negative coordinates... You can't have a pixel at -1,-1.

The decart coordinate start will not be your mouse, it will always be the topleft corner. I guess this is where you got confused

Yes you can, if you are interested I can pm you. Since this is off topic.

Linear algebra and Analytic geometry actually studies how its done. Later there is integral geometrical equatations, but I hardly understand that.

On Topic: @I just rechecked the code I posted here. Yes it doesn't exactly work ;; I messed up the the correct numbers (Coord0, Coords) I guess I didnt correctly edit it yesterday, althought I remember re-editting it because there was a mistake... Gosh I feel stupid now

Anyway, for simplicitys sake:

```Dim \$Coords[2], \$Coords0[2], \$X[2], \$Y[2]
\$Coords[0] = @desktopwidth
\$Coords[1] = 0
\$Coords0[0] = 0
\$Coords0[1] = @desktopheight

\$Pos = MouseGetPos()

\$Distance1=Sqrt((\$Coords[0]-\$Pos[0])^2+(\$Coords[1]-\$Pos[1])^2)
\$Distance2=Sqrt((\$Coords0[0]-\$Pos[0])^2+(\$Coords0[1]-\$Pos[1])^2)

If \$Distance1 < \$Distance2 Then
MsgBox( 0, "", "Dot 1 is closer" )
Else
MsgBox( 0, "", "Dot 2 is closer" )
EndIf```

Top right = dot1

Top left = dot2

I apologise to martin and tpt, I don't know why I didn't post the correctly edited code. Perhaps I was just sleepy. Although it does say "This post has been edited by Qousio: Yesterday, 11:20 PM" Weird :/

No worries Qousio, we're on the same planet again now

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

##### Share on other sites

Please don't use Qousio's code from the earlier post, it's complete gibberish.

The code you don't understand, ie the code Uez posted, is what you need. If you have a right angled triangle then lets say that the length of the sloping line is H, and the length of the other two sides are A and O. then the relationship is

H*H = A*A + O*O

which is nice and simple.

If you draw a straight line from the cursor at x,y to a point at D,E, and draw a vertical line through the point and a horizontal line through the cursor position you get a right-angled triangle.

The sloping line is H which we want to find.

The horizontal side length is A= x - D

The vertical side is O = y - E

so we can say

H*H = A*A + O*O

or

H = Sqrt(A*A + O*O)

Of course, if x = D and y = E then the result is 0. That's all Uez's function does.

Since you are only interested in which point is nearer, there is no need to find the square root because if the square root is smalle then so is the square, so you could save time by ignoring the square root and work out A*A + O*O for each point.

Thanks for the explanation

UEZ

Please don't send me any personal message and ask for support! I will not reply!

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

##### Share on other sites

Thanks to all who helped me. It works like a charm now.

Edited by tpt

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

×

• Wiki

• Back

• Git

## Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.