Assuming A
, B
& C
are arrays then you can assemble an object with your bounding values like so:
bounds_a = {
start: Math.min.apply(null, A),
end: Math.max.apply(null, A),
}
You could even make a class for this to help:
var Bounds = function(src){
this.min = Math.min.apply(null, src)
this.max = Math.max.apply(null, src)
this.size = Math.abs(max - min + 1) //+1 since we're dealing with integers and need to account for the lowest value
}
Bounds.prototype.intersects = function(target){
rangeMin = Math.min(this.min, target.min)
rangeMax = Math.max(this.max, target.max)
range = Math.abs(rangeMax - rangeMin)
width = this.size + target.size
return range < width
}
A better explanation of the overlap logic can be found here - What's the most efficient way to test two integer ranges for overlap?
Testing this:
var A = <array 1...10>
var B = <array 7...20>
var C = <array 3...5>
var a = new Bounds(A) //min: 1, max: 10, size: 10
var b = new Bounds(B) //min: 7, max: 20, size: 14
var c = new Bounds(C) //min: 3, max: 5, size: 3
a.intersects(b) //true
b.intersects(c) //false
a.intersects(c) //true