代码之家  ›  专栏  ›  技术社区  ›  Emotional_Goose

Java求线的交点,并找到它的坐标和它右边或下面的最近邻

  •  0
  • Emotional_Goose  · 技术社区  · 7 年前

    我正在努力寻找一种方法来获得坐标x和坐标Y的最近点,如果它们不存在的话。

    public class CheckForIntersection {
    
        double x1, x2, x3, x4, y1, y2, y3, y4, a, b, c, d, e, f, checkLinear, x, y;
    
        CheckForIntersection(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4) {
            this.x1 = x1;
            this.y1 = y1;
            this.x2 = x2;
            this.y2 = y2;
            this.x3 = x3;
            this.y3 = y3;
            this.x4 = x4;
            this.y4 = y4;
    
            checkintersection();
        }
    
        public double getx() {
            return x;
        }
    
        public double gety() {
            return y;
        }
    
        public void checkintersection() {
            a = y1 - y2;
            b = -(x1 - x2);
            c = y3 - y4;
            d = -(x3 - x4);
            e = (y1 - y2) * x1 - (x1 - x2) * y1;
            f = (y3 - y4) * x3 - (x3 - x4) * y3;
    
            checkLinear = (a * d) - (b * c);
    
            x = ((e * d) - (b * f)) / checkLinear;
            y = ((a * f) - (e * c)) / checkLinear;
    
            System.out.println(a + " " + b + " " + c + " " + d + " " + e + " " + f);
            checknearestNeighbour(x, y);
    
            if (checkLinear == 0) {
                System.out.println("the intersection is parallel");
            } else {
                System.out.println("X coordinate:" + x + " X coordinate:" + y);
            }
        }
    }
    

    以下参数的结果应为2.88889,1.1111。

    1 回复  |  直到 7 年前
        1
  •  2
  •   ChrisN    7 年前

    我假设“网格”是指这样的结构:

    x - x - x
    |   |   |   
    x - o - o
    |   |   |     
    o - x - x                                                                                                                  
    

    其中x=节点&o=无节点 (假设所有交叉点之间的距离相等)

    1. 要确定所有音符的坐标,需要一个起始点(坐标原点)。 这可能是网格的左上角或左下角,由您决定。

    2. 然后你可以一行一行地循环(一个循环用于水平线,一个循环用于每条线中的元素),检查每个交叉点是否有节点(这个ofc需要一种确定交叉点的方法)。因为它是一个网格(相同的距离),你可以运行一个简单的计数器来计算你的步数,并确定一条线中每个交点的坐标。如果找到一个节点,请存储这些信息

    3. 为了提高效率,您现在可以在遍历网格时回答问题,也可以在收集信息后在单独的步骤中回答问题。