/** * Computes the intersection between two lines. The calculated point is approximate, * since integers are used. If you need a more precise result, use doubles * everywhere. * (c) 2007 Alexander Hristov. Use Freely (LGPL license). http://www.ahristov.com * * @param x1 Point 1 of Line 1 * @param y1 Point 1 of Line 1 * @param x2 Point 2 of Line 1 * @param y2 Point 2 of Line 1 * @param x3 Point 1 of Line 2 * @param y3 Point 1 of Line 2 * @param x4 Point 2 of Line 2 * @param y4 Point 2 of Line 2 * @return Point where the segments intersect, or null if they don't */ public Point intersection( int x1,int y1,int x2,int y2, int x3, int y3, int x4,int y4 ) { int d = (x1-x2)*(y3-y4) - (y1-y2)*(x3-x4); if (d == 0) return null; int xi = ((x3-x4)*(x1*y2-y1*x2)-(x1-x2)*(x3*y4-y3*x4))/d; int yi = ((y3-y4)*(x1*y2-y1*x2)-(y1-y2)*(x3*y4-y3*x4))/d; return new Point(xi,yi); }