Jump to content

2D Array autofill


Recommended Posts

Hello,

im quite new to programming and AutoIT so i hope i can make clear what my problem is.

I wrote a function for something im working on which gets called now and than, and returns an Array consisting of 2 elements. ( They are coordinates of certain points in a picture ) Eg : ( 150 , 680 ). Each time this function runs, the output of those elements would be different. So next time it runs, it could give ( 350, 243 ). I wanted to write a second function, which accepts those coords, and does some basic calculation with them. In my example i would have to check these with eachother to see if they reside in a maximum radius of 256 pixels of eachother. As i was thinking about this, i figured i should use a 2D array and fill it up untill i have something like this :

Dim $CoordArray[4][4]

Func StoreCoords( $CoordX, $CoordY )

$CoordArray[0][0] = $CoordX
$CoordArray[0][1] = $CoordY
$CoordArray[1][0] = $CoordX
$CoordArray[1][1] = $CoordY

_ArrayDisplay( $CoordArray )

EndFunc

The problem i seem to be running into is that i cant find a proper way to fill the 2D array in a decent way. I know in advance what the maximum size of the array will be, so thats not a problem. But the StoreCoords() function receives 2 new coords, everytime it gets called ( so maximum 4 times ) and should store them on a new row. And thats exactly where im stuck, i dont know how i should make it so the function gets 2 coords, stores them in an array @ row 0, gets 2 new coords, stores them @ row 1, ... and so on... Soon as i get that working i would have to be able to find a way to compare $CoordArray[0][0] with $CoordArray[1][0] and see if the difference between them is <= 256 pixels, than do it for the Y coord. If both of these are True, than we have a set of coords at maximum distance of 256 pixels found...

I hope i explained a bit what my problem is, if needed i can elaborate a bit more.

PS : Im not native english, so excuse me for any language errors.

Thx in advance,

Ariff

Link to comment
Share on other sites

Something like this?

#include <Array.au3>

Global $CoordArray[ 4 ] [ 2 ]

StoreCoords( 123 , 987 )
StoreCoords( 234 , 876 )
StoreCoords( 345 , 765 )
StoreCoords( 456 , 654 )

Func StoreCoords( Const $CoordX , Const $CoordY )
    Static Local $tab = 0

    $CoordArray[ 0 + $tab ] [ 0 ] = $coordX
    $CoordArray[ 0 + $tab ] [ 1 ] = $CoordY

    $tab += 1

    _ArrayDisplay( $CoordArray )
EndFunc
Edited by LaCastiglione
Link to comment
Share on other sites

Heh, i feel blessed by the simplicity of your solution, it works perfectly :) I tend to go look for too complicated solutions, when simply increasing the $tab value here does the job. Thanks a lot for your swift and fast reply LaCastiglione. Much appreciated !

Something like this?

#include <Array.au3>

Global $CoordArray[ 4 ] [ 2 ]

StoreCoords( 123 , 987 )
StoreCoords( 234 , 876 )
StoreCoords( 345 , 765 )
StoreCoords( 456 , 654 )

Func StoreCoords( Const $CoordX , Const $CoordY )
    Static Local $tab = 0

    $CoordArray[ 0 + $tab ] [ 0 ] = $coordX
    $CoordArray[ 0 + $tab ] [ 1 ] = $CoordY

    $tab += 1

    _ArrayDisplay( $CoordArray )
EndFunc

Link to comment
Share on other sites

Well now i have all this working nicely, i bumped into a new problem :) Been trying to figure this one out myself but i still have a lot to learn and could use a hand...

Take the previous example where we have an $CoordsArray[4][2]. Each row is filled with coordinates x and y. All of these coords, should stay into a radius of lets say 128 pixels. So basically i would have to check each coord against eachother, because together they have to be in a max range of 128 pixels of eachother. So whenever a coord doesnt match that condition, it can be dropped. If for example 3 out of the 4 coords test positive against this rule ( $MaxRange = 128 ) i would consider it a success, otherwise not.

Usually what happens when i write my questions on a forum like this, i get the solution from writing a problem description and try to translate that into a script, but this time im really stuck at how to test the coords against eachother to see if they fit the $MaxRange rule.

Any ideas on how i could have a go at this ?

thanks again,

Ariff

Link to comment
Share on other sites

Look into the comparison operators. <= >= < > =

Something like this?

#include <Array.au3>

Global $CoordArray[ 4 ] [ 2 ]

StoreCoords( 123 , 987 )
StoreCoords( 234 , 876 )
StoreCoords( 345 , 765 )
StoreCoords( 456 , 654 )

Func StoreCoords( Const $CoordX , Const $CoordY )
    Static Local $tab = 0

    $CoordArray[ $tab ] [ 0 ] = $coordX
    $CoordArray[ $tab ] [ 1 ] = $CoordY

    $tab += 1

    If $CoordArray[ $tab ] [ 0 ] > 128 Then
        ; do something
    EndIf
    
    If $CoordArray[ $tab ] [ 1 ] > 128 Then
        ; do something
    EndIf
    
    _ArrayDisplay( $CoordArray )
EndFunc
Edited by LaCastiglione
Link to comment
Share on other sites

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

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...