问题描述
更新:此问题正在寻求有关如何为任何给定坐标获取一组邻居的指导。
Update: this question is seeking guidance on how to get a set of neighbors for any given coordinate.
我创建了一个包含坐标的二维数组,
I created a 2d array that contains coordinates,
int[][] coordinates= { { -1, -1 }, { -1, 0 }, { -1, +1 },
{ 0, -1 }, { 0, +1 }, { +1, -1 }, { +1, 0 }, { +1, -1 } };
正如您所知,这些是坐标(0,0)的邻居。
As you can tell, these are the neighbors for coordinates (0,0).
现在我正在尝试实现一个带有两个参数(int positionX,int positionY)
的方法,并使用输入参数值coordiante(x,y)作为起始坐标,找到该坐标的所有邻居。
Now I am trying to implement a method that takes two parameters (int positionX, int positionY)
, and use the input parameters value coordiante(x,y) as the starting coordinate and find all the neighbors for this coordinate.
我在考虑这样的事情:
int getNearCoordinates(int positionX, int positionY) {
for (int[] coordinate: coordinates) {
//I am not sure what to do after this
}
}
我正在尝试使用循环从我创建的2d数组中获取单个坐标,我被困在这里。我如何找到一种方法来正确找到positionX和positionY的邻居?
I am trying to use a loop to get the individual coordinate from the 2d array I created and I am stuck at here. How do I find a way to appropriately find positionX's and positionY's neighbor?
什么是邻居?
下图中的所有橙色点都是Origin的邻居(0,0)
All orange points in diagram below are neighbours of Origin (0,0)
推荐答案
我建议
- 使用专用的类(
坐标
)而不是int []
。这使您的代码更容易扩展(第三维等)或更改(使用double
而不是int
等)。在示例中,您可以看到 imutable 类 - 这会阻碍代码产生副作用。 - 使用
集合
而不是数组
。这使处理变得更容易(你只需添加
和删除
项目) - 使用 java8-Streaming-API 。它快速闪电,使您的代码更易读。
- Use a dedicated class (
Coordinate
) instead ofint[]
. This makes your code easier to extend (3rd dimention, etc) or to change (usingdouble
instead ofint
, etc.). In the example you can see an imutable class - this hinders code to have side effects. - Use
Collection
instead ofArray
. This makes handling much easier (you can simplyadd
andremove
items) - Use java8-Streaming-API. It is lightning fast and makes your code better readable.
其他想法:
- 您甚至可以将
getNearCoordinates
作为Coordinate类的一部分。这将使新的Coordinate(27,35).getNearCoordinates()
可用。 - 而不是存储
x
和y
在单独的字段中,您还可以使用Map< Axis,Integer>
。这会使您的代码更难理解 - 但会减少重复的代码。 - 您还可以使用两个嵌套循环生成方向集合
for(int x = -1; x< = 1; x ++)for(int y = -1; y< = 1; y ++)use(new Coordinate(x,y))
。这会使你的代码更干净,但可能更难理解。
- You could even make
getNearCoordinates
part of the Coordinate class. This would makenew Coordinate(27,35).getNearCoordinates()
available. - Instead of storing
x
andy
in separate fields you could also use aMap<Axis, Integer>
. This would make your code a little bit harder to understand - but would reduce duplicated code. - You could also generate the collection of directions by using two nested loops
for (int x = -1; x <= 1; x++) for (int y = -1; y <= 1; y++) use(new Coordinate(x,y))
. This would make your code cleaner, but might be harder to understand.
示例代码:
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());
}
}
没有Java8-streaming API的相同行为看起来像这样:
Same behaviour without Java8-streaming API would look like this:
/** @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;
}
这篇关于如何为任何给定坐标找到正确的邻居?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!