Here you can find the source of intersect(Point a, Point b, Point c, Point d)
public static Point intersect(Point a, Point b, Point c, Point d)
//package com.java2s; //License from project: Apache License import java.awt.Point; public class Main { public static Point intersect(Point a, Point b, Point c, Point d) { float[] rv = intersect(new float[] { a.x, a.y, b.x, b.y, c.x, c.y, d.x, d.y }); if (rv == null) return null; return new Point((int) rv[0], (int) rv[1]); }/* w ww . ja v a2s . c om*/ public static float[] intersect(float[] points) { float[] l1 = getAffineFunction(points[0], points[1], points[2], points[3]); float[] l2 = getAffineFunction(points[4], points[5], points[6], points[7]); float[] crossing; if (l1 == null && l2 == null) { return null; } else if (l1 == null && l2 != null) { crossing = intersect(l2[0], l2[1], points[0]); } else if (l1 != null && l2 == null) { crossing = intersect(l1[0], l1[1], points[4]); } else { crossing = intersect(l1[0], l1[1], l2[0], l2[1]); } if (crossing == null) { return null; } if (!(inBounds(points[0], points[1], points[2], points[3], crossing[0], crossing[1]) && inBounds(points[4], points[5], points[6], points[7], crossing[0], crossing[1]))) return null; return crossing; } public static float[] intersect(float m1, float b1, float m2, float b2) { if (m1 == m2) { return null; } float x = (b2 - b1) / (m1 - m2); float y = m1 * x + b1; return new float[] { x, y }; } public static float[] intersect(float m1, float b1, float x) { return new float[] { x, m1 * x + b1 }; } public static float[] getAffineFunction(float x1, float y1, float x2, float y2) { if (x1 == x2) return null; float m = (y1 - y2) / (x1 - x2); float b = y1 - m * x1; return new float[] { m, b }; } public static boolean inBounds(float bx1, float by1, float bx2, float by2, float x, float y) { if (bx1 < bx2) { if (x < bx1 || x > bx2) return false; } else { if (x > bx1 || x < bx2) return false; } if (by1 < by2) { if (y < by1 || y > by2) return false; } else { if (y > by1 || y < by2) return false; } return true; } }