public static double overlap(double x1, double x2, double x3, double x4) {
        if (x3 < x1) {
            if (x4 < x1) {
                return 0;
            } else {
                return Math.min(x2, x4) - x1;
            }
        } else {
            if (x2 < x3) {
                return 0;
            } else {
                return Math.min(x2, x4) - x3;
            }
        }
    }
public static double overlap ( double x1 , double x2 , double x3 , double x4 ) { if ( x3 < x1 ) { if ( x4 < x1 ) { return 0 ; } else { return Math . min ( x2 , x4 ) - x1 ; } } else { if ( x2 < x3 ) { return 0 ; } else { return Math . min ( x2 , x4 ) - x3 ; } } }
Returns overlap between lines [x1, x2] and [x3. x4].