如何为任何给定坐标找到正确的邻居?

OPK*_*OPK 7 java algorithm coordinates

更新:这个问题正在寻求如何获得任何给定坐标的一组邻居的指导.

我创建了一个包含坐标的二维数组,

 int[][] coordinates= { { -1, -1 }, { -1, 0 }, { -1, +1 },
            { 0, -1 }, { 0, +1 }, { +1, -1 }, { +1, 0 }, { +1, -1 } };
Run Code Online (Sandbox Code Playgroud)

如您所知,这些是坐标(0,0)的邻居.

现在我尝试实现一个接受两个参数的方法(int positionX, int positionY),并使用输入参数值coordiante(x,y)作为起始坐标并找到该坐标的所有邻居.

我在考虑这样的事情:

    int getNearCoordinates(int positionX, int positionY) {

        for (int[] coordinate: coordinates) {
               //I am not sure what to do after this
        }

    } 
Run Code Online (Sandbox Code Playgroud)

我试图使用一个循环从我创建的2d数组获取单个坐标,我被困在这里.我如何找到一种方法来正确找到positionX和positionY的邻居?

什么是邻居?

下图中的所有橙色点都是Origin的邻居 (0,0) 在此输入图像描述

sla*_*dan 6

我建议

  • 使用专用的(Coordinate)而不是int[].这使您的代码更容易扩展(第三维等)或更改(使用double而不是int等).在示例中,您可以看到一个可模拟的类 - 这会阻碍代码产生副作用.
  • Collection而不是Array.这使处理更容易(你可以简单地addremove项目)
  • 使用java8-Streaming-API.它快速闪电,使您的代码更易读.

其他想法:

  • 你甚至可以getNearCoordinates参加Coordinate课程.这将new Coordinate(27,35).getNearCoordinates()提供.
  • 取而代之的存储xy在不同的领域,你也可以使用Map<Axis, Integer>.这会使您的代码更难理解 - 但会减少重复的代码.
  • 您还可以使用两个嵌套循环生成方向集合for (int x = -1; x <= 1; x++) for (int y = -1; y <= 1; y++) use(new Coordinate(x,y)).这将使您的代码更清晰,但可能更难理解.

示例代码:

import java.util.*;
import java.util.stream.Collectors;

public class Snippet {

    // make a class to be more flexible
    class Coordinate {

        // final fields are making this an "imutable"
        final int x;
        final int y;

        /** constructor to take coordinate values */
        Coordinate(int x, int y) {
            this.x = x;
            this.y = y;
        }

        /** moves this coordinate by another coordinate */
        Coordinate move(Coordinate vector) {
            return new Coordinate(x + vector.x, y + vector.y);
        }
    }

    /** using Collection instead of Array makes your live easier. Consider renaming this to "directions". */
    Collection<Coordinate> coordinates = Arrays.asList(
            new Coordinate( -1, -1 ), // left top
            new Coordinate( -1,  0 ), // left middle
            new Coordinate( -1, +1 ), // left bottom
            new Coordinate(  0, -1 ), // top
            new Coordinate(  0, +1 ), // bottom
            new Coordinate( +1, -1 ), // right top
            new Coordinate( +1,  0 ), // right middle
            new Coordinate( +1, +1 )  // right bottom
            );

    /** @return a collection of eight nearest coordinates near origin */
    Collection<Coordinate> getNearCoordinates(Coordinate origin) {
        return
                // turn collection into stream
                coordinates.stream()

                // move the origin into every direction
                .map(origin::move)

                // turn stream to collection
                .collect(Collectors.toList());
    }

}
Run Code Online (Sandbox Code Playgroud)

没有Java8-streaming API的相同行为如下所示:

/** @return a collection of eight nearest coordinates near origin */
Collection<Coordinate> getNearCoordinates(Coordinate origin) {
    Collection<Coordinate> neighbours = new ArrayList<>();

    for (Coordinate direction : coordinates)
        neighbours.add(origin.move(direction));

    return neighbours;
}
Run Code Online (Sandbox Code Playgroud)