Ionut Flavius Pogacian
Ionut Flavius Pogacian

Reputation: 4801

Generate a matrix using random x and y coords in Javascript

I am trying to generate a matrix.

The width and height of the matrix are unknown.

Only at the end of the process i will know the min(x,y) and the max(x,y)

I need the matrix to be accessed like m1[2] and not m[1,2]

Each element should save a object.

How can i generate the matrix as in php?

this is what i have so far, but i cant access or save data because it gives error, because the height property is unknown:

matrix[row][min_column].height;

image

my code:

var matrix = new Array();

for (i = lastDrawn.length - 1; i >= 0; i--) {

            ind++;
//            console.log(tile.x);
//            matrix[tile.x, tile.y] = 0;

            tile = lastDrawn[ i ];

            if (!matrix[tile.y])
                matrix[tile.y] = new Array();

            if (!matrix[tile.y][tile.x])
                matrix[tile.y][tile.x] = new Array();

            //calculare tile cu min x,y si max x,y pt viewport curent
            var size_x = Math.round(tile.size.x);//tile width
            var size_y = Math.round(tile.size.y);//tile height
            var position_x = tile.position.x;
            var position_y = tile.position.y;
            var min = OpenSeaDragonCustomSettings.save_viewport_min(tile.y, tile.x, size_x, size_y, position_x, position_y);
            var max = OpenSeaDragonCustomSettings.save_viewport_max(tile.y, tile.x, size_x, size_y, position_x, position_y);
            matrix[tile.y][tile.x] = {'x': tile.x, 'y': tile.y, 'width': size_x, 'height': size_y, 'position_x': position_x, 'position_y': position_y};//y:rand,x:coloana
            console.log(matrix[tile.y][tile.x]);

}

Upvotes: 0

Views: 476

Answers (1)

MichaC
MichaC

Reputation: 13381

You code to create the "matrix" should be fine. The message Uncought TypeError: Cannot read property 'height' of undefined just means that the object you try to access is undefined, not the property... matrix[row][min_column] seems to be undefined, just check row and min_column if those indexes are valid...

Upvotes: 1

Related Questions