# Check if figure fits in coordinates

## Recommended Posts

Hello.

I'm struggling with mathematical function.

I have an array with size \$array[12][12] that looks like on the image below.

Red squares represents taken space.

I need to create a function that would find first free coordinates that would fit object of typed \$width and \$height.

Example

\$width 1, \$height 1 should return  \$return = [ 0, 0 ]

\$width 2, \$height 3 should return  \$return = [ 0, 2 ]

\$width 3, \$height 1 should return  \$return = [ 0, 0 ]

Any help is appreciated

##### Share on other sites

I did something similar to this a while back when I was searching for an array within an array.

You'll need to go through your source array, each row checking each column. When you've hit a valid index in the source array (a white space) then you want to create another two nested for loops to go through the source array at the current row and column that you found the first valid spot to whatever width and height you specify.

```Func FindFirstFree(Const ByRef \$aSource, Const ByRef \$iW, Const ByRef \$iH)
For \$row = 0 to UBound(\$aSource, \$UBOUND_ROWS) - 1
For \$column = 0 to UBound(\$aSource, \$UBOUND_COLUMNS) - 1
Local \$valid = False

If (The row and column of the \$aSource is a valid white space) Then
For \$i = \$row to \$iH + \$row
For \$p = \$column to \$iW + \$column

Next
Next
EndIf
Next
Next
EndFunc```

This should get you started. When you get to the If (The row and column....) part of the script do some error checking to make sure that \$row + \$iH or \$column + \$iW are not going to go out of bounds of the \$aSource array.

##### Share on other sites

Do you try anything yet ?
Please post what you have to we be able to better understand your needs.

Signature beginning:   Wondering who uses AutoIT and what it can be used for ?
*
How to use IE.au3  UDF with  AutoIt v3.3.14.x  for other useful stuff click the following button

Spoiler

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind.

My contribution to others projects or UDF based on  others projects: * _sql.au3 UDF  * POP3.au3 UDF *  RTF Printer - UDF * XML.au3 UDF * SMTP Mailer UDF * Dual Monitor resolution detection * * 2GUI on Dual Monitor System * _SciLexer.au3 UDF *

"Homo sum; humani nil a me alienum puto" - Publius Terentius Afer
"Program are meant to be read by humans and only incidentally for computers and execute" - Donald Knuth, "The Art of Computer Programming"
, be   and       \\//_.

Anticipating Errors :  "Any program that accepts data from a user must include code to validate that data before sending it to the data store. You cannot rely on the data store, ...., or even your programming language to notify you of problems. You must check every byte entered by your users, making sure that data is the correct type for its field and that required fields are not empty."

Signature last update: 2018-10-31

##### Share on other sites

I make it working already thanks to InnunoTaishou code. Here's the code for anyone that may need it in the future

```Func CanFitItem(Const ByRef \$aSource, Const ByRef \$iW, Const ByRef \$iH)
Local \$ret[2] = [ -1, -1 ]

Local \$maxColumn = UBound(\$aSource, \$UBOUND_COLUMNS) - 1
Local \$maxRow = UBound(\$aSource, \$UBOUND_ROWS) - 1

For \$column = 0 to \$maxColumn
For \$row = 0 to \$maxRow
;Check if space is free
If \$aSource[\$row][\$Column] Then
Local \$valid = True

For \$p = \$column to \$iW + \$column - 1
If \$p > \$maxColumn Then
\$valid = False
ExitLoop
EndIf

For \$i = \$row to \$iH + \$row - 1
If \$i > \$maxRow Or Not \$aSource[\$i][\$p] Then
\$valid = False
ContinueLoop 3 ;Going to the next row
EndIf
Next
Next

If \$valid Then
\$ret[0] = \$row
\$ret[1] = \$column
Return \$ret
EndIf
EndIf
Next
Next

Return \$ret
EndFunc```

##### Share on other sites

I hope this works properly:

```#include <Array.au3>
\$s = ChrW(9609)

Global \$aTest[][] = [["",   "",     "",     \$s,     "",     "",     "",     "",     "",     "",     "",     ""], _
["",   \$s,     "",     \$s,     "",     "",     \$s,     "",     "",     "",     "",     ""], _
["",   "",     \$s,     \$s,     "",     "",     "",     \$s,     "",     "",     "",     ""], _
["",   "",     \$s,     "",     "",     "",     "",     \$s,     \$s,     "",     "",     ""], _
["",   "",     \$s,     "",     "",     "",     "",     "",     \$s,     \$s,     "",     ""], _
["",   \$s,     \$s,     "",     "",     "",     "",     "",     "",     \$s,     "",     ""], _
["",   "",     \$s,     "",     "",     "",     \$s,     "",     "",     \$s,     "",     ""], _
["",   "",     "",     \$s,     "",     "",     \$s,     "",     "",     "",     \$s,     ""], _
["",   "",     "",     \$s,     "",     "",     "",     \$s,     "",     "",     "",     ""], _
["",   \$s,     "",     "",     "",     "",     "",     \$s,     "",     "",     "",     ""], _
[\$s,   \$s,     "",     "",     "",     "",     \$s,     "",     "",     "",     "",     ""], _
[\$s,   \$s,     "",     "",     "",     "",     "",     \$s,     "",     "",     "",     ""]]

\$iW = 2
\$iH = 12
\$aResult = FindFirstFit(\$aTest, \$iW, \$iH, \$s)
If Not @error Then
For \$iY = \$aResult[0][0] To \$iY + \$iH - 1
For \$iX = \$aResult[0][1] To \$iX + \$iW - 1
\$aTest[\$iY][\$iX] = ChrW(9587)
Next
Next
Else
ConsoleWrite(@error & @CRLF)
EndIf
_ArrayDisplay(\$aTest)

Func FindFirstFit(ByRef \$aArray, \$iW, \$iH, \$sChar = ChrW(9619))
If BitOR(Not \$iW, Not \$iH) Then Return SetError(1, 0, 0)
If BitOR(\$iW > UBound(\$aArray, 2), \$iH > UBound(\$aArray)) Then Return SetError(2, 0, 0)
Local \$iX = 0, \$iY = 0, \$bFound = True, \$aResult[1][2]
\$iW -= 1
\$iH -= 1
Do
\$bFound = True
For \$a = \$iY To \$iY + \$iH
For \$b = \$iX To \$iX + \$iW
If \$aArray[\$a][\$b] = \$sChar Then
\$bFound = False
ExitLoop 2
EndIf
Next
Next
If \$bFound Then
\$aResult[0][0] = \$iY
\$aResult[0][1] = \$iX
ExitLoop
EndIf
\$iX += \$iW + 1
If \$iX + \$iW > UBound(\$aArray, 2) Then
\$iX = 0
\$iY += \$iH + 1
If \$iY + \$iH > UBound(\$aArray) Then ExitLoop
EndIf
Until False
If \$bFound Then Return \$aResult
Return SetError(3, 0, 0)
EndFunc```

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!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

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