Java Line Intersect intersectLines(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4, double[] point)

Here you can find the source of intersectLines(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4, double[] point)

Description

Checks whether line (x1, y1) - (x2, y2) and line (x3, y3) - (x4, y4) intersect.

License

Apache License

Return

1 if two lines intersect in the defined interval, otherwise 0.

Declaration

public static int intersectLines(double x1, double y1, double x2, double y2, double x3, double y3, double x4,
        double y4, double[] point) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

public class Main {
    public static final double EPSILON = Math.pow(10, -14);

    /**//from   w w  w .  ja va2 s.  co m
     * Checks whether line (x1, y1) - (x2, y2) and line (x3, y3) - (x4, y4) intersect. If lines
     * intersect then the result parameters are saved to point array. The size of {@code point}
     * must be at least 2.
     *
     * @return 1 if two lines intersect in the defined interval, otherwise 0.
     */
    public static int intersectLines(double x1, double y1, double x2, double y2, double x3, double y3, double x4,
            double y4, double[] point) {
        double A1 = -(y2 - y1);
        double B1 = (x2 - x1);
        double C1 = x1 * y2 - x2 * y1;
        double A2 = -(y4 - y3);
        double B2 = (x4 - x3);
        double C2 = x3 * y4 - x4 * y3;
        double coefParallel = A1 * B2 - A2 * B1;
        // double comparison
        if (x3 == x4 && y3 == y4 && (A1 * x3 + B1 * y3 + C1 == 0) && (x3 >= Math.min(x1, x2))
                && (x3 <= Math.max(x1, x2)) && (y3 >= Math.min(y1, y2)) && (y3 <= Math.max(y1, y2))) {
            return 1;
        }
        if (Math.abs(coefParallel) < EPSILON) {
            return 0;
        }
        point[0] = (B1 * C2 - B2 * C1) / coefParallel;
        point[1] = (A2 * C1 - A1 * C2) / coefParallel;
        if (point[0] >= Math.min(x1, x2) && point[0] >= Math.min(x3, x4) && point[0] <= Math.max(x1, x2)
                && point[0] <= Math.max(x3, x4) && point[1] >= Math.min(y1, y2) && point[1] >= Math.min(y3, y4)
                && point[1] <= Math.max(y1, y2) && point[1] <= Math.max(y3, y4)) {
            return 1;
        }
        return 0;
    }
}

Related

  1. intersection(Line2D lineA, Line2D lineB)
  2. intersection(Point2D p1, Point2D p2, Point2D p3, Point2D p4)
  3. intersectionPoint(final Line2D l1, final Line2D l2)
  4. intersectionZJU(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4)
  5. intersectionZJU(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4)
  6. intersectLineSegments(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4)
  7. intersectLinesWithParams(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4, double[] params)
  8. intersects(int b1, int e1, int b2, int e2)
  9. intersects(int start1, int length1, int start2, int length2)