From c70ff0d95e8220640b9101bd627a3235cbb8e87e Mon Sep 17 00:00:00 2001 From: Zutatensuppe Date: Thu, 15 Apr 2021 21:07:52 +0200 Subject: [PATCH] add function to check if rectangles overlap --- common/Geometry.js | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/common/Geometry.js b/common/Geometry.js index 9eb4323..8dfab23 100644 --- a/common/Geometry.js +++ b/common/Geometry.js @@ -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, }