Created
October 26, 2016 14:41
-
-
Save Scarysize/8c278d47f1b8cbccb5896cc4e335d037 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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