Get rectangles from 2nd array of tiles

Basically I want to convert this input:

var tiles:Array = new Array();

tiles[0] = [1, 1, 1, 1, 0, 0, 0, 0, 0, 0];
tiles[1] = [1, 1, 1, 1, 0, 1, 1, 1, 0, 0];
tiles[2] = [0, 0, 0, 0, 0, 1, 0, 0, 0, 0];
tiles[3] = [0, 0, 0, 0, 0, 1, 0, 0, 0, 0];
tiles[4] = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
tiles[5] = [0, 0, 1, 0, 0, 0, 0, 0, 0, 0];
tiles[6] = [0, 0, 0, 0, 0, 1, 1, 1, 1, 1];
tiles[7] = [0, 0, 0, 0, 0, 1, 1, 1, 1, 1];
tiles[8] = [0, 0, 0, 0, 0, 1, 1, 1, 1, 1];
tiles[9] = [0, 0, 0, 0, 0, 1, 1, 1, 1, 1];

      

To this conclusion (vector of rectangles):

[
    (x=0, y=0, w=4, h=2),
    (x=5, y=1, w=3, h=1),
    (x=5, y=2, w=1, h=2),
    (x=2, y=5, w=1, h=1),
    (x=5, y=6, w=5, h=4)
]

      

Also it cannot be overlapping rectangles and it can be in any layout not only the one I gave.

I have looked for code samples in as3 or algorithms, but they are always in other programming languages, or difficult to implement, so I hope someone else here already had the same problem.

EDIT: As @Marty suggested, another answer might work the other way around by providing rectangles and getting an array as output, I'll try to get it to work and put it as an answer (if no one else works first), but I hope it can be done the first way.

+3


source to share


2 answers


I'll try to work on a solution for your original problem, but while you want to convert some input rectangles to a 2D array, you can try this function I did:

function rectanglesToArray(input:Vector.<Rectangle>):Array
{
    var r:Rectangle;
    var gwidth:int = 0;
    var gheight:int = 0;

    // Determine the width and height of the grid.
    for each(r in input)
    {
        gwidth = Math.max(r.x + r.width, gwidth);
        gheight = Math.max(r.y + r.height, gheight);
    }


    // Define empty cells.
    var output:Array = [];
    for(var i:int = 0; i < gheight; i++)
    {
        output[i] = [];

        for(var j:int = 0; j < gwidth; j++)
            output[i].push(0);
    }

    // Define filled rectangles.
    for each(r in input)
    {
        for(var column:int = r.x; column < r.x + r.width; column++)
        {
            for(var row:int = r.y; row < r.y + r.height; row++)
            {
                output[row][column] = 1;
            }
        }
    }

    return output;
}

      

With a little demo:



var result:Array = rectanglesToArray(new <Rectangle>[
    new Rectangle(0, 0, 4, 2),
    new Rectangle(5, 1, 3, 1),
    new Rectangle(5, 2, 1, 2),
    new Rectangle(2, 5, 1, 1),
    new Rectangle(5, 6, 5, 4)
]);

trace(result.join("\n"));

      

Which gives the result:

1,1,1,1,0,0,0,0,0,0
1,1,1,1,0,1,1,1,0,0
0,0,0,0,0,1,0,0,0,0
0,0,0,0,0,1,0,0,0,0
0,0,0,0,0,0,0,0,0,0
0,0,1,0,0,0,0,0,0,0
0,0,0,0,0,1,1,1,1,1
0,0,0,0,0,1,1,1,1,1
0,0,0,0,0,1,1,1,1,1
0,0,0,0,0,1,1,1,1,1

      

+3


source


This is not a very difficult problem, but it is certainly interesting. You can write a function that identifies the rectangle with the given coordinates for the start of the rectangle, and that function can then call a function that changes the grid and zeroes out those contained in the rectangle. Then it's just a matter of iterating over the grid. In C-esque pseudocode:



var grid = {{0, 0, 0....}}
var output = {}

function examine_rectangle(x, y) {
    var w = 1
    var h = 1

    while (grid[y][x + w] == 1) {
        w += 1
    }

    var i = 0

    while (grid[y + h][x + i] == 1) {
        if (i == w - 1) {
            i = 0
            h += 1
        } else {
            i += 1
        }
    }

    output.push({x, y, w, h})

    clean_rectangle(x, y, w, h)
}

function clean_rectangle(x, y, w, h) {
    var i = 0

    while (i < w) i += 1) {
        var j = 0

        while (j < h) {
            grid[y + j][x + i] = 0
            j += 1
        }

        i += 1
    }
}

var x = 0

while (x < grid[0].size()) {
    var y = 0

    while(y < grid.size()) {
        if (grid[y][x] == 1) {
            examine_rectangle(x, y)
        }

        y += 1
    }

    x += 1
}

// yey! now we have got our answer in the output array

      

0


source







All Articles