简体   繁体   中英

Mirroring a 2d array

I have a 2d array thus:

var array = [
    [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]
];

What I basically want to do is when a node is changed, eg

array[3][2] = 1;

It changes the opposite side of the array as well, eg

array[1][2] = 1;

[EDIT] Basically, I select a random x and random y co-ordinate and populate that with a 1 .

Ie

If I changed one 2 nodes from the left, I want to change the same one 2 nodes from the right.

Following on from my comment, create a function to update the data. Then you can calculate the "opposite side" and update that too:

function updateArray(array, x, y, v){
    array[x][y] = v;
    var newX = array.length - 1 - x;
    array[newX][y] = v;
}

usage example:

updateArray(array, 3, 2, 1);//will set array[3][2] and array[1][2]

Here is a working example

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM