Divide a 2D array into multiple arrays using a sep

2020-03-31 04:57发布

I have converted a JavaScript spritesheet to a 2D integer array, and now I'm trying to split a 2D array of integers into multiple 2D arrays, using 1 as the "separator" number.

Spritesheet Is there any way to separate a 2D JavaScript array like the following into multiple arrays using a separator number, as shown below?

function separate2DArray(arrToSeparate, separator){
    //separate the 2D array into multiple 2D arrays, using a
    //specific number as the separator
}

//array to separate:
[
[5, 5, 5, 1, 5, 4, 5],
[5, 5, 4, 1, 4, 3, 4],
[1, 1, 1, 1, 1, 1, 1], //1 is the "separator number", which splits the array
[9, 2, 1, 4, 2, 4, 5],       //horizontally and vertically
]
//The array above would produce the following 2D arrays:

5 5 5
5 5 4

5 4 5
4 3 4

9 2

4 2 4 5

The main application for this algorithm that I have in mind is spritesheet image separation.

2条回答
SAY GOODBYE
2楼-- · 2020-03-31 05:18

You would have to iterate through the array, capturing the preview entries into a sub array whenever you find a 1

http://jsfiddle.net/cWYpr/10/

 var arr = [[5, 5, 5, 1, 5, 4, 5], [5, 5, 4, 1, 4, 3, 4], [1, 1, 1, 1, 1, 1, 1], [9, 2, 1, 4, 2, 4, 5]];
  var twoD = [];
  for (var x = 0; x < arr.length; x++) {
    var row = arr[x];
    var subArray = [], subArrays=[];
    for (var y = 0; y < row.length; y++) {
      if (row[y] == 1) {
        if (subArray.length) subArrays.push(subArray.slice(0));
        subArray = [];
      }
      else {
        subArray.push(row[y]);
      }
    }
    if (subArray.length) subArrays.push(subArray);
    if(subArrays.length) twoD.push(subArrays);
  }
  console.log(twoD);
  document.write(JSON.stringify(twoD));
查看更多
闹够了就滚
3楼-- · 2020-03-31 05:31

Given that the separated areas are rectangular, this will work:

function separate2DArray(array, sep){
    //separate the 2D array into multiple 2D arrays, using a
    //specific number as the separator
    var result = [],
        currentSubs = {}; // using x coordinate as key

    for (var y=0; y<array.length; y++) {
        var line = array[y],
            subBegin = 0;
        for (var x=0; x<=line.length; x++) {
            if (x == line.length || line[x] == sep) {
                if (subBegin < x) {
                    var sub = line.slice(subBegin, x);
                    if (subBegin in currentSubs)
                        currentSubs[subBegin].push(sub);
                    else
                        currentSubs[subBegin] = [sub];
                } else { // a line of separators, subBegin == x
                    if (subBegin in currentSubs) {
                        result.push(currentSubs[subBegin]);
                        delete currentSubs[subBegin];
                    }
                }
                subBegin = x+1;
            }
        }
    }
    for (var begin in currentSubs)
        result.push(currentSubs[begin]);
    return result;
}

The result here is just a very simple array of the subareas, without any information about their position in the original area. Improved version:

function separate2DArray(array, sep){
    var result = [],
        currentSubs = {};
    for (var y=0; y<array.length; y++) {
        var line = array[y],
            subBegin = 0;
        for (var x=0; x<=line.length; x++) {
            if (x == line.length || line[x] == sep) {
                if (subBegin < x) {
                    var subline = line.slice(subBegin, x);
                    if (! (subBegin in currentSubs)) {
                        var subarea = [];
                        result.push({x:x, y:y, area:subarea});
                        currentSubs[subBegin] = subarea;
                    }
                    currentSubs[subBegin].push(subline);
                } else {
                    if (subBegin in currentSubs)
                        delete currentSubs[subBegin];
                }
                subBegin = x+1;
            }
        }
    }
    return result;
}
查看更多
登录 后发表回答