Java - 将计划分为几个部分

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

最佳答案

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

相关文章:

java - java中添加到arrayList时,arrayList中的所有元素均为null

math - 找到圆上的切点?

lisp - 10 个 LISP 原语类似于欧几里德几何的 5 个公理?

Java 6 : how to drag/move a line?

Java 翻译点

java - 将自定义消息从 Servlet 传递到 JSP 页面?

java - 跟踪调用另一个类时使用变量的位置

c# - 二维线段与矩形的交集

azure - 在 VPN P2S 下连接到 Azure 资源

java - 为什么可以在循环中创建具有相同变量名的线程对象?