Here you can find the source of intersectLineSegments(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4)
public static double[] intersectLineSegments(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4)
//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 }; } }