public static int intersectLine ( double x1 , double y1 , double x2 , double y2 , double rx1 , double ry1 , double rx2 , double ry2 ) { // LEFT/RIGHT/UP if ( ( rx2 < x1 && rx2 < x2 ) || ( rx1 > x1 && rx1 > x2 ) || ( ry1 > y1 && ry1 > y2 ) ) { return 0 ; } // DOWN if ( ry2 < y1 && ry2 < y2 ) { } else { // INSIDE if ( x1 == x2 ) { return CROSSING ; } // Build bound double bx1 , bx2 ; if ( x1 < x2 ) { bx1 = x1 < rx1 ? rx1 : x1 ; bx2 = x2 < rx2 ? x2 : rx2 ; } else { bx1 = x2 < rx1 ? rx1 : x2 ; bx2 = x1 < rx2 ? x1 : rx2 ; } double k = ( y2 - y1 ) / ( x2 - x1 ) ; double by1 = k * ( bx1 - x1 ) + y1 ; double by2 = k * ( bx2 - x1 ) + y1 ; // BOUND-UP if ( by1 < ry1 && by2 < ry1 ) { return 0 ; } // BOUND-DOWN if ( by1 > ry2 && by2 > ry2 ) { } else { return CROSSING ; } } // EMPTY if ( x1 == x2 ) { return 0 ; } // CURVE-START if ( rx1 == x1 ) { return x1 < x2 ? 0 : - 1 ; } // CURVE-END if ( rx1 == x2 ) { return x1 < x2 ? 1 : 0 ; } if ( x1 < x2 ) { return x1 < rx1 && rx1 < x2 ? 1 : 0 ; } return x2 < rx1 && rx1 < x1 ? - 1 : 0 ; }
Returns how many times rectangle stripe cross line or the are intersect