Tormiasz

Check if figure fits in coordinates

5 posts in this topic

Hello.

I'm struggling with mathematical function.

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

issUvus.png

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 this post


Link to post
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 this post


Link to post
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 ?
* GHAPI UDF - modest begining - comunication with GitHub REST API *
ADO.au3 UDF     POP3.au3 UDF     XML.au3 UDF    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 (my own projects): * Debenu Quick PDF Library - UDF * Debenu PDF Viewer SDK - UDF * Acrobat Reader - ActiveX Viewer * UDF for PDFCreator v1.x.x * XZip - UDF * AppCompatFlags UDF * CrowdinAPI UDF * _WinMergeCompare2Files() * _JavaExceptionAdd() * _IsBeta() * Writing DPI Awareness App - workaround * _AutoIt_RequiredVersion() * Chilkatsoft.au3 UDF * TeamViewer.au3 UDF * JavaManagement UDF * VIES over SOAP * WinSCP UDF * GHAPI UDF - modest begining - comunication with GitHub REST API *

My contribution to others projects or UDF based on  others projects: * _sql.au3 UDF  * POP3.au3 UDF *  RTF Printer - UDF * XML.au3 - BETA * ADO.au3 UDF SMTP Mailer UDF *

Useful links: * Forum Rules * Forum etiquette *  Forum Information and FAQs * How to post code on the forum * AutoIt Online Documentation * AutoIt Online Beta Documentation * SciTE4AutoIt3 getting started * Convert text blocks to AutoIt code * Games made in Autoit * Programming related sites * Polish AutoIt Tutorial * DllCall Code Generator * 

Wiki: Expand your knowledge - AutoIt Wiki * Collection of User Defined Functions * How to use HelpFile * Best coding practices * 

IE Related:  * How to use IE.au3  UDF with  AutoIt v3.3.14.x * Why isn't Autoit able to click a Javascript Dialog? * Clicking javascript button with no ID * IE document >> save as MHT file * IETab Switcher (by LarsJ ) * HTML Entities * _IEquerySelectorAll() (by uncommon) * 

I encourage you to read: * Global Vars * Best Coding Practices * Please explain code used in Help file for several File functions * OOP-like approach in AutoIt * UDF-Spec Questions *  EXAMPLE: How To Catch ConsoleWrite() output to a file or to CMD *

"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"
:naughty:  :ranting:, 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: 2017-06-04

Share this post


Link to post
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 this post


Link to post
Share on other sites

#5 ·  Posted (edited)

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!

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!
¯\_(ツ)_/¯

Share this post


Link to post
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