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)
public static int intersectLines(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4, double[] point)
//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; } }