# how to check if a num is within certain parameters

## Recommended Posts

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)

##### Share on other sites

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.
##### Share on other sites

You should read up how to work with arrays in AutoIT

```Dim \$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```
##### Share on other sites

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!
##### Share on other sites

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 coords

```Func _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```
##### Share on other sites

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 AutoIT

I 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

EndIf

Next

OK, 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:

CODE

If Abs(\$acutalX - \$preferredX) <= 1 And Abs(\$actualY - \$preferredY) <= 1 Then

MsgBox(4096,"Info", "coordinates are okay)

EndIf

I 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

##### Share on other sites

The Abs() function lets you compute a difference without caring if the first number is larger or smaller than the second

If (\$CurCoordX - \$StoredCoord[\$r][0] <= 1 And \$CurCoordX - \$StoredCoord[\$r][0] >= -1) Then

can be rewritten as

If Abs(\$CurCoordX - \$StoredCoord[\$r][0] <= 1) Then

because 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 <= 1

Some examples:

Abs(8 - 5) ;returns 3

Abs(4 - 4) ;returns 0

Abs(5 - 8) ;returns 3

Hope that helps

Edited 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!
##### Share on other sites

The Abs() function lets you compute a difference without caring if the first number is larger or smaller than the second

If (\$CurCoordX - \$StoredCoord[\$r][0] <= 1 And \$CurCoordX - \$StoredCoord[\$r][0] >= -1) Then

can be rewritten as

If Abs(\$CurCoordX - \$StoredCoord[\$r][0] <= 1) Then

because 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 <= 1

Some examples:

Abs(8 - 5)  ;returns 3

Abs(4 - 4)  ;returns 0

Abs(5 - 8)  ;returns 3

Hope that helps

<{POST_SNAPBACK}>

that is perfect, just what i need. thankyou very much

## Create an account

Register a new account

• ### Recently Browsing   0 members

×

• Wiki

• Back

• #### Beta

• Git
• FAQ
×
• Create New...