// line 1: (x0,y0)-(x1,y1), line 2: (x2,y2)-(x3,y3) // intersection point: (outx, outy) // return value: false (no intersection point), true (1 intersection point) // public static bool LineIntersectWithLine( double x0, double y0, double x1, double y1, double x2, double y2, double x3, double y3, ref double outx, ref double outy, ExtendOption option) { double a11 = x3 - x2; double a12 = x0 - x1; double a21 = y3 - y2; double a22 = y0 - y1; double b1 = x0 - x2; double b2 = y0 - y2; double djacob = a11 * a22 - a21 * a12; if (Math.Abs(djacob) < Zero) { return(false);//line parrel, no cross point } double t1 = (a22 * b1 - a12 * b2) / djacob; double t2 = (-a21 * b1 + a11 * b2) / djacob; //calculate cross point outx = x2 + (x3 - x2) * t1; outy = y2 + (y3 - y2) * t1; if (option == ExtendOption.None) { if (t1 > -Zero && t1 < (1 + Zero) && t2 > -Zero && t2 < (1 + Zero)) { return(true); } } else if (option == ExtendOption.This) { if (t1 > -Zero && t1 < (1 + Zero)) { return(true); } } else if (option == ExtendOption.Other) { if (t2 > -Zero && t2 < (1 + Zero)) { return(true); } } else if (option == ExtendOption.Both) { return(true); } return(false); }
// line 1: (x0,y0)-(x1,y1), line 2: (x2,y2)-(x3,y3) // intersection point: (outx, outy) // return value: false (no intersection point), true (1 intersection point) // public static bool LineIntersectWithLine( double x0, double y0, double x1, double y1, double x2, double y2, double x3, double y3, ref double outx, ref double outy, ExtendOption option) { double a11 = x3 - x2; double a12 = x0 - x1; double a21 = y3 - y2; double a22 = y0 - y1; double b1 = x0 - x2; double b2 = y0 - y2; double djacob = a11 * a22 - a21 * a12; if (Math.Abs(djacob) < Zero) return false;//line parrel, no cross point double t1 = (a22 * b1 - a12 * b2) / djacob; double t2 = (-a21 * b1 + a11 * b2) / djacob; //calculate cross point outx = x2 + (x3 - x2) * t1; outy = y2 + (y3 - y2) * t1; if (option == ExtendOption.None) { if (t1 > -Zero && t1 < (1 + Zero) && t2 > -Zero && t2 < (1 + Zero)) return true; } else if (option == ExtendOption.This) { if (t1 > -Zero && t1 < (1 + Zero)) return true; } else if (option == ExtendOption.Other) { if (t2 > -Zero && t2 < (1 + Zero)) return true; } else if (option == ExtendOption.Both) return true; return false; }