This is hopefully a very simple maths question. If I have two number ranges, what is the simplest and most efficient way to check if they clash, eg:
10-20 and 11-14 // clash as B is contained in A
11-15 and 20-22 // don't clash
24-26 and 20-30 // clash as A is contained in B
15-25 and 20-30 // clash as they overlap at each end
I currently have this mess, but there must be a much simpler way to do this check:
$clash = ($b1 >= $a1 && $b1 <= $a2)
|| ($b2 >= $a1 && $b2 <= $a2)
|| ($a1 >= $b1 && $a1 <= $b2)
|| ($a2 >= $b1 && $a2 <= $b2);
Well, first make sure you have well-ordered pairs (probably a good idea anyway, depending on what you plan to do with them):
if($a1 > $a2) {
// swap $a1 and $a2
$temp = $a1;
$a1 = $a2;
$a2 = $temp;
}
if($b1 > $b2) {
// swap $b1 and $b2
$temp = $b1;
$b1 = $b2;
$b2 = $temp;
}
Then you should be able to simplify to:
$clash = ($a2 <= $b1) || ($a1 >= $b2);
Edit: Whoops, got that test backwards! Try:
$clash = !(($a2 <= $b1) || ($a1 >= $b2));
I think it should be as simple as this:
clash = A_LOW <= B_HIGH AND A_HIGH >= B_LOW
example:
10 - 20
4 - 11 // this will clash with above
1 - 5 // this will clash with above
40 - 50
store your numbers in 2 arrays, say
x_array = array(10,4,11,40);
y_array = array(20,11,5,50);
asort($x_array); // sort only the first x range array values and maintain the index
$max_val = -1;
$last_index = 0;
foreach($x_array as $each_index => $each_x){
// get the y corresponding value
$this_y = $y_array[$each_index];
echo "$this_y < $max_val";
if($each_x > $max_val && $this_y > $max_val){
$max_val = $this_y;
}
else{
$last_x = $x_array[$last_index];
$last_y = $y_array[$last_index];
echo "Error: Overlap between: ($each_x,$this_y) and ($last_x,$last_y)";
}
$last_index = $each_index;
}
The ranges DO NOT clash if and only if $a2 <= $b1 or $a1 >= $b2 (assuming that ranges are given as ordered pairs). Now negate the condition.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With