add function to check if rectangles overlap

This commit is contained in:
Zutatensuppe 2021-04-15 21:07:52 +02:00
parent 3342e7087e
commit c70ff0d95e

View file

@ -35,6 +35,22 @@ function rectMoved(rect, x, y) {
}
}
/**
* Returns true if the rectangles overlap, including their borders.
*
* @param {x, y, w, h} rectA
* @param {x, y, w, h} rectB
* @returns bool
*/
function rectsOverlap(rectA, rectB) {
return !(
rectB.x > (rectA.x + rectA.w)
|| rectA.x > (rectB.x + rectB.w)
|| rectB.y > (rectA.y + rectA.h)
|| rectA.y > (rectB.y + rectB.h)
)
}
function rectCenterDistance(rectA, rectB) {
return pointDistance(rectCenter(rectA), rectCenter(rectB))
}
@ -47,4 +63,5 @@ export default {
rectCenter,
rectMoved,
rectCenterDistance,
rectsOverlap,
}