Arathalion Posted March 2, 2005 Share Posted March 2, 2005 I'm trying to evaluate a number; but i can't get it to work properly. (probably another stupid mistype knowing me) I'd like to check to see whether a number in a variable is within 1 of another number on an array. eg, Q:are the co-ordinates 45,98 (from a variable)both within 1 number of 44,94 (from an array)? A:no, they aren't BOTH the answers i would want to be valid in this example would be: 45,93 45,94 45,95 46,93 46,94 46,95 47,93 47,94 47,95 Here's the code i wrote to do this, which isn't working properly: for $r = 0 to $prog if $CurCoordX - $StoredCoord[X][$r] <=1 AND $CurCoordX - $StoredCoord[X][$r] >=-1 then if $CurCoordY - $StoredCoord[Y][$r] <=1 AND $CurCoordY - $StoredCoord[Y][$r] >=-1 then $p = 1 endif next there must be a way a to do this that works. (and effeciency is a high priority) thanks in advance Link to comment Share on other sites More sharing options...
Insolence Posted March 2, 2005 Share Posted March 2, 2005 Well read what you're is actually doing. Do you understand what that code does? PS. I suggest you format your code a little better by capitalizing words "I thoroughly disapprove of duels. If a man should challenge me, I would take him kindly and forgivingly by the hand and lead him to a quiet place and kill him." - Mark TwainPatient: "It hurts when I do $var_"Doctor: "Don't do $var_" - Lar. Link to comment Share on other sites More sharing options...
SumTingWong Posted March 2, 2005 Share Posted March 2, 2005 You should read up how to work with arrays in AutoIT expandcollapse popupDim $StoredCoord[9][2] $StoredCoord[0][0] = 45 $StoredCoord[0][1] = 93 $StoredCoord[1][0] = 45 $StoredCoord[1][1] = 94 $StoredCoord[2][0] = 45 $StoredCoord[2][1] = 95 $StoredCoord[3][0] = 46 $StoredCoord[3][1] = 93 $StoredCoord[4][0] = 46 $StoredCoord[4][1] = 94 $StoredCoord[5][0] = 46 $StoredCoord[5][1] = 95 $StoredCoord[6][0] = 47 $StoredCoord[6][1] = 93 $StoredCoord[7][0] = 47 $StoredCoord[7][1] = 94 $StoredCoord[8][0] = 47 $StoredCoord[8][1] = 95 $CurCoordX = 48 $CurCoordY = 96 For $r = 0 To UBound($StoredCoord)-1 If ($CurCoordX - $StoredCoord[$r][0] <= 1 And $CurCoordX - $StoredCoord[$r][0] >= -1) And _ ($CurCoordY - $StoredCoord[$r][1] <= 1 And $CurCoordY - $StoredCoord[$r][1] >= -1) Then $p = 1 MsgBox(0, "", "YAY!") ExitLoop EndIf Next Link to comment Share on other sites More sharing options...
CyberSlug Posted March 2, 2005 Share Posted March 2, 2005 I'm too lazy to put this in array format, but you proably want to use the absolute value function: If Abs($acutalX - $preferredX) <= 1 And Abs($actualY - $preferredY) <= 1 Then MsgBox(4096,"Info", "coordinates are okay) EndIf Use Mozilla | Take a look at My Disorganized AutoIt stuff | Very very old: AutoBuilder 11 Jan 2005 prototype I need to update my sig! Link to comment Share on other sites More sharing options...
trids Posted March 2, 2005 Share Posted March 2, 2005 I would store the coordinates in a way that makes it easier to check for duplicates. When you store each coordinate, store a "hit-range" along with the actual coordinate (The "hit-range" is a string of delimeted coordinates that qualify for close enough matches). Then when you want to do a proximity check: you convert your lookup coordinates into a format similar to that used for the hit-range; and inspect each entry in the array by comparing your lookup coordinates with the hit-range, using StringInstr(). Example to store the coordsFunc _StoreCoord($pnX, $pnY) ;$gaPoint is a global array (assuming it already has the space we need) of points and a hit-range for each ;$gnPoint_Max is the index of the next unused point. ;This function adds a new point to the $gaPoint array in a format that ;facilitates proximity-scans. Local $sHits, $X, $Y ;Build the hit-range of point in the vicinity $sHits = "" For $X = $pnX -1 to $pnX + 1 For $Y = $pnY - 1 to $pnY + 1 ;build a string of x-y coords (delimited by "|", ;where each point is delimeted by "~" $sHits = $sHits & $X & "|" & $Y & "~" Next Next $sHits = StringTrimRight($sHits,1) ;drop the last "~" $gaPoint[$gnPoint_Max] = $pnX & ";" $pnY & "=" & $sHits ;bump up $gnPoint_Max for the next new entry $gnPoint_Max = $gnPoint_Max + 1 EndFunc .. This allows you to search for a point as follows ..Func _ProximityScan($pnX, $pnY) Local $sScan, $nPt, $ynHit ;Transalate the coords into a scan string $sScan = $pnX & "|" & $pnY ;Find the scan string in the list of points $ynHit = 0 For $nPt = 1 to $gnPoint_Max If StringInstr($gaPoint[$gnPoint_Max], $sScan) Then ;got a hit! $ynHit = 1 ExitLoop Endif Next If $ynHit Then Return $nPt Else Return 0 Endif EndFunc Link to comment Share on other sites More sharing options...
Arathalion Posted March 2, 2005 Author Share Posted March 2, 2005 Well read what you're is actually doing. Do you understand what that code does?PS.I suggest you format your code a little better by capitalizing words <{POST_SNAPBACK}>i DID capitalize my variable names! for the first time ever!! but not the rest. sorry, i will remember that in future. (and yes, i know what my code is meant to do - but it wasn't doing it! but i didn't make it very clear...sorry.)You should read up how to work with arrays in AutoITI know, and i'd love to do that, if i knew where? The manual only shows how to declare them, not how they work or how to use them. For $r = 0 To UBound($StoredCoord)-1 If ($CurCoordX - $StoredCoord[$r][0] <= 1 And $CurCoordX - $StoredCoord[$r][0] >= -1) And _ ($CurCoordY - $StoredCoord[$r][1] <= 1 And $CurCoordY - $StoredCoord[$r][1] >= -1) Then $p = 1 MsgBox(0, "", "YAY!") ExitLoop EndIfNextOK, this is the part i needed, thankyou. i think you got the wrong idea - i didn't explain myself very well at all. The list i listed was a list of answers that should have been valid answers IF i was comparing one set of co-ordinates to the given set. The idea was that i put in random co-ordinates, and compare them to one constant set of co-ordinates, an example of which i gave. But, never mind about the mis-understanding, since it got my thing working anyway.The thing that got it working was the format of the expression: (expression) And_ (expression)i didn't know you could do that - and i haven't seen that anywhere in the help. thankyou I'm too lazy to put this in array format, but you proably want to use the absolute value function:CODEIf Abs($acutalX - $preferredX) <= 1 And Abs($actualY - $preferredY) <= 1 Then MsgBox(4096,"Info", "coordinates are okay)EndIfI don't understand this; i looked at it in the help, but i don't get what it does, or how it could help my problem. but thanks anyway I would store the coordinates in a way that makes it easier to check for duplicates.When you store each coordinate, store a "hit-range" along with the actual coordinate (The "hit-range" is a string of delimeted coordinates that qualify for close enough matches).Then when you want to do a proximity check: you convert your lookup coordinates into a format similar to that used for the hit-range; and inspect each entry in the array by comparing your lookup coordinates with the hit-range, using StringInstr().thanks trids, maybe i'll use that once i've got the darn thing working and want to optimise it.A further explanation of what i DID mean, but didn't explain very well before:Say you have a map, with a grid. On the grid there are several randomly placed dots, and they stay there. Then, dots are randomly placed one by one. IF this new dot is next to one of the original dots, then it stays, and becomes one of the "original dots". Otherwise, it doesn't, and it disappears. hopefully that makes a little more sense Link to comment Share on other sites More sharing options...
CyberSlug Posted March 2, 2005 Share Posted March 2, 2005 (edited) The Abs() function lets you compute a difference without caring if the first number is larger or smaller than the secondIf ($CurCoordX - $StoredCoord[$r][0] <= 1 And $CurCoordX - $StoredCoord[$r][0] >= -1) Thencan be rewritten asIf Abs($CurCoordX - $StoredCoord[$r][0] <= 1) Thenbecause you want to check if the difference between the two x values is less than or equal to one. You only need one test of <= 1Some examples:Abs(8 - 5) ;returns 3Abs(4 - 4) ;returns 0Abs(5 - 8) ;returns 3Hope that helps Edited March 2, 2005 by CyberSlug Use Mozilla | Take a look at My Disorganized AutoIt stuff | Very very old: AutoBuilder 11 Jan 2005 prototype I need to update my sig! Link to comment Share on other sites More sharing options...
Arathalion Posted March 2, 2005 Author Share Posted March 2, 2005 The Abs() function lets you compute a difference without caring if the first number is larger or smaller than the secondIf ($CurCoordX - $StoredCoord[$r][0] <= 1 And $CurCoordX - $StoredCoord[$r][0] >= -1) Thencan be rewritten asIf Abs($CurCoordX - $StoredCoord[$r][0] <= 1) Thenbecause you want to check if the difference between the two x values is less than or equal to one. You only need one test of <= 1Some examples:Abs(8 - 5) ;returns 3Abs(4 - 4) ;returns 0Abs(5 - 8) ;returns 3Hope that helps<{POST_SNAPBACK}>that is perfect, just what i need. thankyou very much 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