Skip to content

Instantly share code, notes, and snippets.

@Scarysize
Created October 26, 2016 14:41
Show Gist options
  • Save Scarysize/8c278d47f1b8cbccb5896cc4e335d037 to your computer and use it in GitHub Desktop.
Save Scarysize/8c278d47f1b8cbccb5896cc4e335d037 to your computer and use it in GitHub Desktop.
function convert(lngLatBounds) {
return {
topLeft: {
lng: lngLatBounds.sw.lng,
lat: lngLatBounds.ne.lat
},
bottomRight: {
lng: lngLatBounds.ne.lng,
lat: lngLatBounds.sw.lat
}
};
}
/**
* Intersection test for two lat/lng bounding boxes.
* src: https://rbrundritt.wordpress.com/2009/10/03/determining-if-two-bounding-boxes-overlap/
* @param {object} a Boundingbox 'a'
* @param {object} b Boundingbox 'b'
* @return {boolean} True if they intersect
*/
export default function intersect(a, b) {
const A = convert(a);
const B = convert(b);
const Atlx = A.topLeft.lng;
const Atly = A.topLeft.lat;
const Abrx = A.bottomRight.lng;
const Abry = A.bottomRight.lat;
const Btlx = B.topLeft.lng;
const Btly = B.topLeft.lat;
const Bbrx = B.bottomRight.lng;
const Bbry = B.bottomRight.lat;
const rABx = Math.abs(Atlx + Abrx - Btlx - Bbrx);
const rABy = Math.abs(Atly + Abry - Btly - Bbry);
const rAxBx = Abrx - Atlx + Bbrx - Btlx;
const rAyBy = Atly - Abry + Btly - Bbry;
if (rABx <= rAxBx && rABy <= rAyBy) {
return true;
}
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment