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.
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.
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/
Given that the separated areas are rectangular, this will work:
The result here is just a very simple array of the subareas, without any information about their position in the original area. Improved version: