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

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

Description

intersect Lines With Params

License

Apache License

Declaration

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

Method Source Code

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

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

    public static int intersectLinesWithParams(double x1, double y1, double x2, double y2, double x3, double y3,
            double x4, double y4, double[] params) {
        double dx = x4 - x3;
        double dy = y4 - y3;
        double d = dx * (y2 - y1) - dy * (x2 - x1);
        // double comparison
        if (Math.abs(d) < EPSILON) {
            return 0;
        }/*from   w  w w.ja v a  2  s.  c o m*/

        params[0] = (-dx * (y1 - y3) + dy * (x1 - x3)) / d;

        if (dx != 0) {
            params[1] = (line(params[0], x1, x2) - x3) / dx;
        } else if (dy != 0) {
            params[1] = (line(params[0], y1, y2) - y3) / dy;
        } else {
            params[1] = 0f;
        }

        if (params[0] >= 0 && params[0] <= 1 && params[1] >= 0 && params[1] <= 1) {
            return 1;
        }

        return 0;
    }

    public static double line(double t, double x1, double x2) {
        return x1 * (1f - t) + x2 * t;
    }
}

Related

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