tpt Posted May 9, 2009 Share Posted May 9, 2009 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. Link to comment Share on other sites More sharing options...
FadetoGrey Posted May 9, 2009 Share Posted May 9, 2009 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. Link to comment Share on other sites More sharing options...
CodyBarrett Posted May 9, 2009 Share Posted May 9, 2009 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..? [size="1"][font="Tahoma"][COMPLETED]-----[FAILED]-----[ONGOING]VolumeControl|Binary Converter|CPU Usage| Mouse Wrap |WinHide|Word Scrammbler|LOCKER|SCREEN FREEZE|Decisions Decisions|Version UDF|Recast Desktop Mask|TCP Multiclient EXAMPLE|BTCP|LANCR|UDP serverless|AIOCR|OECR|Recast Messenger|AU3C|Tik-Tak-Toe|Snakes & Ladders|BattleShips|TRON|SNAKE_____________________[u]I love the Helpfile it is my best friend.[/u][/font][/size] Link to comment Share on other sites More sharing options...
tpt Posted May 9, 2009 Author Share Posted May 9, 2009 (edited) 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 May 9, 2009 by tpt Link to comment Share on other sites More sharing options...
Qousio Posted May 9, 2009 Share Posted May 9, 2009 (edited) 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 May 9, 2009 by Qousio Link to comment Share on other sites More sharing options...
UEZ Posted May 9, 2009 Share Posted May 9, 2009 (edited) You need to calculate the distance between the points. Look here: http://www.autoitscript.com/forum/index.php?showtopic=93417Func 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_DistanceUEZ Edited May 9, 2009 by UEZ Please don't send me any personal message and ask for support! I will not reply! Selection of finest graphical examples at Codepen.io The own fart smells best! ✌Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!¯\_(ツ)_/¯ ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ Link to comment Share on other sites More sharing options...
tpt Posted May 9, 2009 Author Share Posted May 9, 2009 (edited) 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 May 9, 2009 by tpt Link to comment Share on other sites More sharing options...
Qousio Posted May 9, 2009 Share Posted May 9, 2009 (edited) 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 May 9, 2009 by Qousio Link to comment Share on other sites More sharing options...
UEZ Posted May 9, 2009 Share Posted May 9, 2009 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! Selection of finest graphical examples at Codepen.io The own fart smells best! ✌Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!¯\_(ツ)_/¯ ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ Link to comment Share on other sites More sharing options...
Qousio Posted May 9, 2009 Share Posted May 9, 2009 (edited) Look here for an explanation! -> http://gwycon.com/calculating-the-distance...een-two-pixels/UEZNo need to give links really, its simple as one two three.If $A is the mouse coords and $B, $C are the two dots. ThenThe 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 May 9, 2009 by Qousio Link to comment Share on other sites More sharing options...
CodyBarrett Posted May 9, 2009 Share Posted May 9, 2009 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 [size="1"][font="Tahoma"][COMPLETED]-----[FAILED]-----[ONGOING]VolumeControl|Binary Converter|CPU Usage| Mouse Wrap |WinHide|Word Scrammbler|LOCKER|SCREEN FREEZE|Decisions Decisions|Version UDF|Recast Desktop Mask|TCP Multiclient EXAMPLE|BTCP|LANCR|UDP serverless|AIOCR|OECR|Recast Messenger|AU3C|Tik-Tak-Toe|Snakes & Ladders|BattleShips|TRON|SNAKE_____________________[u]I love the Helpfile it is my best friend.[/u][/font][/size] Link to comment Share on other sites More sharing options...
tpt Posted May 9, 2009 Author Share Posted May 9, 2009 its not xD its a math forumla lol but i guess if you've used it before then i would be simpleIm far more confused than i were before.. Link to comment Share on other sites More sharing options...
martin Posted May 9, 2009 Share Posted May 9, 2009 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 isH*H = A*A + O*Owhich 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 - DThe vertical side is O = y - Eso we can sayH*H = A*A + O*Oor 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. Link to comment Share on other sites More sharing options...
Qousio Posted May 10, 2009 Share Posted May 10, 2009 (edited) 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 isH*H = A*A + O*Owhich 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 - DThe vertical side is O = y - Eso we can sayH*H = A*A + O*Oor 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 + zksqrt(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)=1You can also find the length outside of decart coordinates, and even when you don't know where point A and point B is. Edited May 10, 2009 by Qousio Link to comment Share on other sites More sharing options...
martin Posted May 10, 2009 Share Posted May 10, 2009 (edited) 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 May 10, 2009 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. Link to comment Share on other sites More sharing options...
Qousio Posted May 10, 2009 Share Posted May 10, 2009 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 :/ Link to comment Share on other sites More sharing options...
martin Posted May 10, 2009 Share Posted May 10, 2009 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. Link to comment Share on other sites More sharing options...
UEZ Posted May 10, 2009 Share Posted May 10, 2009 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 isH*H = A*A + O*Owhich 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 - DThe vertical side is O = y - Eso we can sayH*H = A*A + O*Oor 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! Selection of finest graphical examples at Codepen.io The own fart smells best! ✌Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!¯\_(ツ)_/¯ ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ Link to comment Share on other sites More sharing options...
tpt Posted May 10, 2009 Author Share Posted May 10, 2009 (edited) Thanks to all who helped me. It works like a charm now. Edited May 10, 2009 by tpt Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now