(Javascript - 数组)获得最左和最正确的连接角色 [英] (Javascript - Arrays) Get most left and most right connected character
问题描述
我有一个数组(2d矩阵),我想得到
I have an array (2d-matrix) and I'd like to get the x/y values for the
- 最左边的x / y值&安培;顶部连接'1'字符
- 最右边&底部连接'1'字符
编辑2.0:
EDIT 2.0:
我用参数x / y调用我的函数,这是我的start-'1'字符的坐标。
I call my function with the parameters x/y, that are the coordinates of my start-'1'-character.
10001
00001
11111
01110 --> (x: 1, y: 3)
我的功能检查上面的列&如果有一个字符'1',它会计算x或y(无论找到哪一列)加上1。
And my function checks the column above & and the column right so if there is a character '1' it counts x or y (wherever the column has been found) plus 1.
我的函数从特定点开始(例如y:2,x:0)
My function starts at a specific point (e.g. y: 2, x: 0)
var array = [
'00000',
'01111', --> get position of the most top & right '1'-character (x: 4, y: 1)
'11000',
'00000'
]
这是获取'1'字符右上角的函数:
This is the function to get the top-right end of '1'-characters:
var array = [
'00000',
'01111',
'11000',
'00000'
]
Array.prototype.get_top_right = function(x_pos, y_pos) {
var matrix = this, y1= y_pos;
for (var x1 = x_pos; x1 < this[0].length; x1++) {
try {
if (matrix[(y1-1)][x1] == '1') y1--;
else if (matrix[y1][(x1+1)] != '1') break;
} catch(e) {}
}; return [x1,y1]
}
var result=array.get_top_right(0,2)
console.log(result)
好的。上面的函数似乎工作正常,但现在我想转过程来获得我的数组/ 2D矩阵的最后一个左下角连接'1'字符。
Ok. The function above seems to work fine, but now I'd like to turn around the process to get the last bottom-left connected '1'-character of my array / 2D-matrix.
var array = [
'00000',
'01111',
'11000', --> get position of the most bottom & left '1'-character (x: 0, y: 2)
'00000'
]
我不知道如何编辑上面的函数以获得左右匹配作为结果,而不是像你上面看到的最正确和最匹配。
I have no clue how to edit the function above to get the left&bottom match as result instead of the most right&top match like you can see above.
编辑1.0我编码的函数直到现在还没有工作但看起来像这样:
Edit 1.0 My function I've coded till yet is not working but looks like this:
Array.prototype.get_bottom_left = function(x_pos, y_pos) {
var matrix = this, y2= y_pos;
for (var x2 = x_pos; x2 > 0; x2--) {
try {
if (matrix[(y2+1)][x2] == '1') y2++;
if (matrix[y2][(x2-1)] != '1') break;
} catch(e) {}
}; return [x2,y2]
}
使用此功能上面和下面的error_array获取数组的左下角连接字符将导致浏览器崩溃。很好!
Using this function above and the error_array below to get the bottom left connected character of the array will lead to a browser crash. Nice!
var error_array = [
'000000',
'000011',
'111110',
'111111'
]
但是,我希望有人可以帮我更新我的功能...
However, I hope somebody can help me updating my function...
提前一百万谢谢,
问候 - hans。
Thanks a million in advance,
Greetings - hans.
推荐答案
我创建了两个版本的 get_bottom_left
方法:
I created two versions of get_bottom_left
method:
-
bottom_left_up
从(x,y)指向左边的点并向上 -
bottom_left_down
从(x,y)指向右侧和下方。
bottom_left_up
which traversing from (x, y) point to the left and to the upbottom_left_down
which traversing from (x, y) point to the right and to the down.
以下是实施:
Array.prototype.bottom_left_up = function(x, y) {
if(this[y][x] === '0') {
return;
}
while(y >= 0) {
while(--x >= 0) {
if(this[y][x] === '0') {
return [x + 1, y];
}
}
if(--y === -1 || this[y][this[y].length - 1] === '0') {
return [0, y + 1];
}
x = this[y].length;
}
};
Array.prototype.bottom_left_down = function(x, y) {
if(this[y][x] === '0') {
return;
}
while(y < this.length) {
if(this[y].indexOf('0', x) !== -1) {
return [x, y];
}
if(++y === this.length || this[y][0] === '0') {
return [x, y - 1];
}
x = 0;
}
};
你看,没有超出范围的保护,它可以单独添加没有问题。让我们测试逻辑:
You see, there are no out-of-range protections, it could be added with no problem separately. Let's test the logic:
var array = [
'00000',
'01111',
'11000',
'00000'
];
console.log(array.bottom_left_up(2, 1)); // [1, 1]
console.log(array.bottom_left_down(2, 1)); // [0, 2]
var array2 = [
'000000',
'000011',
'111110',
'111111'
];
console.log(array2.bottom_left_up(3, 3)); // [0, 3]
console.log(array2.bottom_left_down(3, 3)); // [3, 3]
关于方法保护,我不会使用 try-catch
,我建议如下:
Regarding methods protection, I would not use try-catch
, I would suggest something like:
function(x, y) {
x = parseInt(x, 10);
y = parseInt(y, 10);
if(!this.length || isNaN(x) || isNaN(y) || x < 0 || y < 0 || x >= this.length || y >= this[0].length) {
return;
}
// ...
}
所以你将在3种情况下得到未定义:空数组,坏参数,未找到。
So you will get 'undefined' in 3 cases: empty array, bad params, not found.
这篇关于(Javascript - 数组)获得最左和最正确的连接角色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!