简体   繁体   中英

Is there a way to check if 2 lines are intersecting, and what is the intersection point?

With the help of @Alex, I'v managed to create this following code:

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

The problem is, that I don't really know whether the lines have only one intersection or more. I don't know which more things I have to do and which conditions to check to verify they have only ONE intersection.

I have to say that the lines don't have to be infinite. Which means one line can start where the second one ends, and they will have also the same slope ("m") and also only one intersection.

Also, I want to learn how to send the intersection point correctly because I think I did it wrong and sent it wrongly to the second function.

This method finds intersection of two straight lines (all cases) and returns an intersection point.

If the input lines are parallel or coincide, null Point is returned.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM