Returns the intersection between two line segmentss defined by p0, p1 and q0, q1. If the lines are not intersecting null is returned.
Source
static Point lineIntersection(Point p0, Point p1, Point q0, Point q1) { double epsilon = 1e-10; Vector2 r = new Vector2(p1.x - p0.x, p1.y - p0.y); Vector2 s = new Vector2(q1.x - q0.x, q1.y - q0.y); Vector2 qp = new Vector2(q0.x - p0.x, q0.y - p0.y); double rxs = cross2(r, s); if (rxs.abs() < epsilon) { // The lines are linear or collinear return null; } double t = cross2(qp, s) / rxs; double u = cross2(qp, r) / rxs; if ((0.0 <= t && t <= 1.0) && (0.0 <= u && u <= 1.0)) { return new Point(p0.x + t * r.x, p0.y + t * r.y); } // No intersection between the lines return null; }