Java Line Intersect intersectLineSegments(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4)

Here you can find the source of intersectLineSegments(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4)

Description

intersect Line Segments

License

Apache License

Declaration

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

Method Source Code

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

public class Main {
    public static double[] intersectLineSegments(double x1, double y1, double x2, double y2, double x3, double y3,
            double x4, double y4) {

        final double denominator = (y4 - y3) * (x2 - x1) - (x4 - x3) * (y2 - y1);
        if (denominator == 0.d) {
            return null; // lines are parallel
        }//from w  ww . j  a v  a 2s  .co m

        final double ua = ((x4 - x3) * (y1 - y3) - (y4 - y3) * (x1 - x3)) / denominator;
        if (ua <= 0.d || ua >= 1.d) {
            return null; // no intersection
        }

        final double ub = ((x2 - x1) * (y1 - y3) - (y2 - y1) * (x1 - x3)) / denominator;
        if (ub <= 0.d || ub >= 1.d) {
            return null; // no intersection
        }

        return new double[] { x1 + ua * (x2 - x1), y1 + ua * (y2 - y1), ua };
    }
}

Related

  1. intersection(Point2D p1, Point2D p2, Point2D p3, Point2D p4)
  2. intersectionPoint(final Line2D l1, final Line2D l2)
  3. intersectionZJU(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4)
  4. intersectionZJU(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4)
  5. intersectLines(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4, double[] point)
  6. intersectLinesWithParams(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4, double[] params)
  7. intersects(int b1, int e1, int b2, int e2)
  8. intersects(int start1, int length1, int start2, int length2)
  9. intersects(int x0, int y0, int w0, int h0, int x, int y, int w, int h)