Java 有没有办法检查两条线是否相交,以及相交点是什么?

Java 有没有办法检查两条线是否相交,以及相交点是什么?,java,math,geometry,line,Java,Math,Geometry,Line,在@Alex的帮助下,我成功地创建了以下代码: // Returns true if the lines intersect, false otherwise public boolean isIntersecting(Line other) { if (equals(other)){ return false; } double x11 = this.start.getX(); double y

在@Alex的帮助下,我成功地创建了以下代码:

  // Returns true if the lines intersect, false otherwise
    public boolean isIntersecting(Line other) {
        if (equals(other)){
            return false;
        }
        double x11 = this.start.getX();
        double y11 = this.start.getY();
        double x12 = this.end.getX();
        double y12 = this.end.getY();

        double x21 = other.start.getX();
        double y21 = other.start.getY();
        double x22 = other.end.getX();
        double y22 = other.end.getY();

        // special handling may be needed when x11 == x12
        double m1 = (y12 - y11) / (x12 - x11);
        double b1 = (x11 * y12 - x12 * y11) / (x12 - x11);

        // special handling may be needed when x21 == x22
        double m2 = (y22 - y21) / (x22 - x21);
        double b2 = (x21 * y22 - x22 * y21) / (x22 - x21);

        if ((long) m1 == (long) m2) {
           if (this.start == other.start)
               return true;
           if (other.start == other.end)
               return true;
            if (other.start == this.end)
                return true;
            if (other.start == this.start)
                return true;
           return false;
        }
        double x = (b2 - b1)/(m1 - m2);
        double y = m1 * x + b1;  // or m2 * x + b2
        if (x>x11 && x<x12 && y<y11 && y>y12 && x>x21 && x<x22 && y<y21 && y>y22) {
            Point.intersection = new Point(x, y);
            return true;
        }
        return false;
    }
    // Returns the intersection point if the lines intersect,
    // and null otherwise.
    public Point intersectionWith(Line other) {
        if (isIntersecting(other)) {
            return Point.intersection;
        }
        return null;
    }
//如果线相交,则返回true,否则返回false
公共布尔值isIntersecting(行其他){
如果(等于(其他)){
返回false;
}
double x11=this.start.getX();
double y11=this.start.getY();
double x12=this.end.getX();
double y12=this.end.getY();
double x21=other.start.getX();
double y21=other.start.getY();
double x22=other.end.getX();
double y22=other.end.getY();
//当x11==x12时,可能需要特殊处理
双m1=(y12-y11)/(x12-x11);
双b1=(x11*y12-x12*y11)/(x12-x11);
//当x21==x22时,可能需要特殊处理
双m2=(y22-y21)/(x22-x21);
双b2=(x21*y22-x22*y21)/(x22-x21);
如果((长)m1==(长)m2){
if(this.start==other.start)
返回true;
if(other.start==other.end)
返回true;
if(other.start==this.end)
返回true;
if(other.start==this.start)
返回true;
返回false;
}
双x=(b2-b1)/(m1-m2);
双y=m1*x+b1;//或m2*x+b2

如果(x>x11&&xx21&&x此方法查找两条直线的交点(所有情况下),并返回交点

如果输入线平行或重合,则返回
null

    public static Point findIntersection(Line line1, Line line2) {
        double x11 = line1.getX1();
        double y11 = line1.getY1();
        double x12 = line1.getX2();
        double y12 = line1.getY2(); 

        double x21 = line2.getX1();
        double y21 = line2.getY1();
        double x22 = line2.getX2();
        double y22 = line2.getY2();

        if (x11 == x12 && x21 == x22) {  // both lines are constant x
            if (x11 == x21) {
                System.out.println("Lines coincide");
            } else {
                System.out.println("Lines are parallel to each other and axis 0Y");
            }
            // no intersection point
            return null;

        } else if (x11 == x12 || x21 == x22) { // either line is constant x
            double x;
            double m;
            double b;
            if (x11 == x12) { // first line is constant x, second is sloped
                x = x11;
                m = (y22 - y21) / (x22 - x21);
                b = (x22 * y21 - x21 * y22) / (x22 - x21);
            } else { // second line is constant x, first is sloped
                x = x21;
                m = (y12 - y11) / (x12 - x11);
                b = (x12 * y11 - x11 * y12) / (x12 - x11);
            }
            double y = m * x + b;

            System.out.printf("Lines intersect in (%.2f, %.2f)%n", x, y);

            return new Point(x, y);

        } else { // both lines are sloped
            double m1 = (y12 - y11) / (x12 - x11);
            double b1 = (x12 * y11 - x11 * y12) / (x12 - x11);

            double m2 = (y22 - y21) / (x22 - x21);
            double b2 = (x22 * y21 - x21 * y22) / (x22 - x21);

            if (m1 == m2) {
                if (b1 == b2) {
                    System.out.println("Sloped lines coincide");
                } else {
                    System.out.println("Lines are parallel with slope " + m1);
                }
                // no intersection point
                return null;
            }
            // calculating intersection coordinates
            double x = (b2 - b1)/(m1 - m2);
            double y = m1 * x + b1;  // or m2 * x + b2

            System.out.printf("Lines intersect in (%.2f, %.2f)%n", x, y);

            return new Point(x, y);
        }
    }

如果已经有了起点和终点,x1、x2、y1和y2的用途是什么?除非它们是平行的,否则它们总是相交的。你可以计算满足条件的x的交点。m1*x+b1=m2*x+b2,然后检查x值是否在x1和x2值之间。应该有帮助:@bramhaag我没有起点和终点,我是ini使用this.start=新点(x1,y1)this.end=新点(x2,y2)时,请不要将对象与
=
运算符(例如,在
if(this.start==other.start)
等)进行比较,您需要重写并使用
equals
非常感谢!我还添加了当第1行和第2行合并并且基本上是同一行时会发生什么情况的选项。嘿@Alex,有没有“拆分”的方法将其分为两个不同的函数?第一个函数将根据两条直线是否相交返回true或false,第二个函数将找到交点?我的意思是,我可以在两个不同的函数中做相同的数学运算,但效率很低。我正试图找到一种方法来找到交点,而不需要得到“m”做和你在第一个函数中做的一样的数学运算。你认为呢?因为从数学上讲,我们不需要找到“m”(抱歉,忘了这个词)为了找到交点。你的代码只检查两条线是否平行。但这还不足以证明它们有交点。我们必须检查两条线上的交点是否都在,你不觉得吗?你说的检查两条线上的交点是否都在是什么意思?欧几里德上的两条线一个平面要么在一个点上相交,要么平行。我们被告知不能期望直线是无限的,所以我不能仅仅通过比较它们的斜率“m”来决定它们是相交的还是平行的.我的意思是你引用的那句话,我们可以找到交点,然后检查它的X&Y值是否在两条线上,以100%知道它是交点。