使用边界条件检查2d数组中的附近点

时间:2017-07-25 23:42:23

标签: javascript arrays

我有一个程序可以检查数组数组中的坐标,并搜索附近的坐标,找到最近的5个事件'。但是,当在网格的边缘时,在这个例子中(0,0),我面临着多次返回相同事件但是距离不同的问题。离开(这个距离是曼哈顿的距离)。

我认为这是因为我有参数设置,如果它想要检查的坐标在网格之外(小于0),它的值被改变以匹配边界(0)。

let check = (x, y, d) => {
        if (x > 20) {
            x = 20;
        }
        if (x < 0) {
            x = 0;
        }
        if (y > 20) {
            y = 20;
        }
        if (y < 0) {
            y = 0;
        }

        if (Main[x][y].event) {
            let info = {
                x: x - (xRange/2),
                y: y - (xRange/2),
                event: Main[x][y].event,
                distance: d,
                ticket: Main[x][y].tickets[0],
            }
            return info;
        } else {
            return false;
        }
    }

let findEvents = (x, y) => {
        let nearby = [];
        let info;

        // Check point x, y
        if (Main[x][y].event) {
            info = {
                    x: x - (xRange/2),
                    y: y - (xRange/2),
                    event: Main[x][y].event,
                    distance: 0,
                    tickets: Main[x][y].tickets,
            }
            nearby.push(info);
        }

        for (let d = 1; d <= 40; d++) {
            for (let i = 0; i < d + 1; i++) {

                info = check(x - d + i, y - i, d);

                if (info) {
                    nearby.push(info);
                }
                if ((nearby.length > 5) &&
                    (nearby[(nearby.length-1)].distance !=
                        nearby[(nearby.length-2)].distance)) {
                    return nearby.slice(0,-1);
                }

                info = check(x + d - i, y + i, d);

                if (info) {
                    nearby.push(info);
                }
                if ((nearby.length > 5) &&
                    (nearby[(nearby.length-1)].distance !=
                        nearby[(nearby.length-2)].distance)) {
                    return nearby.slice(0,-1);
                }
            }

            for (let i = 1; i < d; i++) {

                info = check(x - i, y + d - i, d);

                if (info) {
                    nearby.push(info);
                }
                if ((nearby.length > 5) &&
                    (nearby[(nearby.length-1)].distance !=
                        nearby[(nearby.length-2)].distance)) {
                        return nearby.slice(0,-1);
                }

                info = check(x + d - i, y - i, d);

                if (info) {
                    nearby.push(info);
                }
                if ((nearby.length > 5) &&
                    (nearby[(nearby.length-1)].distance !=
                        nearby[(nearby.length-2)].distance)) {
                        return nearby.slice(0,-1);
                }
            }
        }
        return nearby;
    }

有关如何避免这种情况的任何提示? (或者通常清理我的代码:D)

1 个答案:

答案 0 :(得分:1)

我还没有完全理解你的find_events方法,但似乎要避免你描述的问题,只要该点位于网格外,你就应该从check返回false。因此:

let check = (x, y, d) => {
  if (x > 20 || x < 0 || y > 20 ||y < 0) {
    return false;
  }
...