1

I am trying to figure out a way to slice out a 2d array section from a 2d array.

I am looking for an efficient way to create another 2d array from the coordinate locations (4,5) to (7,10). The area you see as 1s in the array.

var arr = [
    [0,0,0,0,0,0,0,0,0,0,0],
    [0,0,0,0,0,0,0,0,0,0,0],
    [0,0,0,0,0,0,0,0,0,0,0],
    [0,0,0,0,0,0,0,0,0,0,0],
    [0,0,0,0,0,1,1,1,1,1,0],
    [0,0,0,0,0,1,1,1,1,1,0],
    [0,0,0,0,0,1,1,1,1,1,0],
    [0,0,0,0,0,0,0,0,0,0,0],
];

var successfulCropOfArray = [
    [1,1,1,1,1],
    [1,1,1,1,1],
    [1,1,1,1,1]
];

Does anybody have any pseudo code available for how I could accomplish this. Here is what I've tried with little to no success:

var slicedMap = Create2DArray(visibleMap);
var topLeftCorner = {
    x: 4,
    y: 5
};
var bottomRightCorner = {
    x: 10,
    y: 7
};

for(var left = topLeftCorner.x;left<bottomRightCorner.x;left++) {   //from left to right scan
    for(var top = bottomRightCorner.y-visibleMap;top>bottomRightCorner.y;top--) {
        for(var slicedX = 0;slicedX<slicedMap.length;slicedX++) {
            for(var slicedY = 0;slicedY<slicedMap.length;slicedY++) {
                slicedMap[slicedY][slicedX] = rooms[newroom].map[bottom][left];
            }
        }
    }
}

5 Answers 5

1

I'm not sure I understand how the coordinates (4,5) to (8,7) would give you that result, but here's an example using an [x1, y1], [x2, y2] syntax:

function getSlice(upper, lower) {
    // grab all the rows within our bounds
    return arr.slice(upper[1], lower[1])
        // and then map each row to the columns in our bounds
        .map(function(row) {
           return row.slice(upper[0], lower[0]); 
        });
}

// usage: getSlice([x1, y1], [x2, y2]);
getSlice([5,4], [10,7]);

Example: http://jsfiddle.net/cvxs05qo/1/


Edit: I accidentally implemented this as [y1, x1], [y2, x2] on first attempt... fixed now.

Sign up to request clarification or add additional context in comments.

1 Comment

Changed coords. question was written in a hurry.
1

I'm going to use some jargon here, because that will help you in future searches on the subject.

Your nested loop is unnecessary. You should think of the upper left corner as your translation point, so (0, 0) translates to (8, 7) in your source coordinate space. The transformation (translation) you're applying, is subtracting the (8, 7) vector from your source point, i.e.: (8, 7) - (8, 7) = (0, 0). In other words, subtract 8 from each of the x values and 7 from each of the y values and you're set.

Remember that your two coordinate spaces don't need any extra transformation, which means that any step right in one space, means the same step right in the other. So, besides the translation you don't need to fiddle with the data.

The size of the source rectangle defines the boundaries of the loop, not the size of the coordinate space it is in; that is irrelevant (It doesn't really matter how big the arrays are, as long as they're not smaller than the area you're searching for).

Comments

0

Just off the top of my head you could do something like this:

    var numberForArray1 = 0;
    var numberForArray2 = 0;

     for (var i = 4; i < 8;i ++)
     {

         for (var j = 5; j < 8; j++)
        {
         successfulCropOfArray[numberForArray1,numberForArray2] = arr[i,j];
         numberForArray1++;


        }

        numberForArray2++; 
        }

Comments

0

I don't know how you got your result from your coordinates, using the concept you mention though, I would use slice

var arr = [
    [0,0,0,0,0,0,0,0,0,0,0],
    [0,0,0,0,0,0,0,0,0,0,0],
    [0,0,0,0,0,0,0,0,0,0,0],
    [0,0,0,0,0,0,0,0,0,0,0],
    [0,0,0,0,0,1,1,1,1,1,0],
    [0,0,0,0,0,1,1,1,1,1,0],
    [0,0,0,0,0,1,1,1,1,1,0],
    [0,0,0,0,0,0,0,0,0,0,0],
];

var successfulCropOfArray = [
    [1,1,1,1,1],
    [1,1,1,1,1],
    [1,1,1,1,1]
];

var topLeftCorner = {
    x: 5,
    y: 4
};
var bottomRightCorner = {
    x: 10,
    y: 7
};
var temp=arr.slice(topLeftCorner.y,bottomRightCorner.y);
for(i=0;i<temp.length;i++){
  temp[i]=temp[i].slice(topLeftCorner.x,bottomRightCorner.x);
}


console.log(JSON.stringify(temp));
console.log(JSON.stringify(successfulCropOfArray));

1 Comment

I didn't get the successful crop. I was just showing what the the desired result would look like.
0

Simple for with Array.prototype.slice

function slice2d(arr, top, left, bottom, right) {
    var a = new Array(bottom - top), i;
    for (i = top; i < bottom; ++i)
        a[i - top] = arr[i].slice(left, right);
    return a;
}

slice2d(arr, 4, 5, 7, 10);
/*
[
    [1,1,1,1,1],
    [1,1,1,1,1],
    [1,1,1,1,1]
]
*/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.