Java - 将计划划分为多个部分

时间:2017-04-28 14:11:45

标签: java geometry point plane

考虑到xy坐标的一个点,我想知道它附近的哪个部分。

检查此图片:

我的观点是这样的:

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)));
    }
}

1 个答案:

答案 0 :(得分:1)

您的图片显示了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)