给定一个带有 x
、y
坐标的点,我想了解它附近的点位于哪个扇区。
检查此图像:
我的观点是这样的:
public class Node {
public final int id;
public final double coordinates[];
public ArrayList<Node> candidateList;
public Node(int id,double... values){
this.id=id;
this.coordinates=values;
}
public int distance(Node other){
double result = 0;
for (int x = 0; x<this.coordinates.length;x++){
result+=(this.coordinates[x]-other.coordinates[x])*(this.coordinates[x]-other.coordinates[x]); //TODO time difference with pow
}
return (int) Math.round(Math.sqrt((result)));
}
}
最佳答案
您的图片显示了8个扇区,您可以通过三个简单的条件找到扇区号(x和y是相对于中心的坐标):
x < 0
y < 0
abs(x) < abs(y)
这三个条件给出了三位信息来定义2^3=8
可能的状态(扇区号)。因此,您只需制作一个表格,将条件结果的每个组合与扇区编号相匹配。例如:
0 0 1 => your sector 1 //x positive, y positive, abs(y)>abs(x)
0 1 0 => your sector 3
如果您想要更多扇区,使用基于角度的方法会更简单。例如,对于 16 个扇区:
//note reverse argument order due to your sector numbering
angle = atan2(x, y)
if angle < 0 then
angle = angle + 2 * Pi
sector = 1 + Floor(angle * 8.0 / Pi)
关于Java - 将计划分为几个部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43682308/