将一个二维数组到多个阵列使用分隔 [英] Divide a 2D array into multiple arrays using a separator

查看:113
本文介绍了将一个二维数组到多个阵列使用分隔的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经转换一个JavaScript spritesheet到2D整数数组,现在我试图分裂整数成多个二维数组的二维数组,使用1作为分隔符号。

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.


有没有什么办法来分离2D JavaScript数组就像使用分隔数以下到多个阵列,如下图所示?

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

这个算法,我心目中的主要应用是spritesheet图像分离。

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

推荐答案

鉴于分离的区域是矩形,这将工作:

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;
}

这篇关于将一个二维数组到多个阵列使用分隔的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆