From 143d8e78437336136bf397b05e02410ccde185a4 Mon Sep 17 00:00:00 2001 From: assiduous Date: Tue, 8 Dec 2020 11:45:19 -0800 Subject: MathLib: added CheckBox2DBox2DOverlap function --- Common/interface/AdvancedMath.hpp | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) (limited to 'Common/interface') diff --git a/Common/interface/AdvancedMath.hpp b/Common/interface/AdvancedMath.hpp index 8d1119e5..11af8e34 100644 --- a/Common/interface/AdvancedMath.hpp +++ b/Common/interface/AdvancedMath.hpp @@ -935,6 +935,38 @@ void RasterizeTriangle(Vector2 V0, } } + +/// Checks if two 2D-boxes overlap. + +/// \tparam [in] AllowTouch - Whether to consider two boxes overlapping if +/// they only touch at their boundaries or corners. +/// \tparam [in] T - Component type. +/// +/// \param [in] Box0Min - Min corner of the first box. +/// \param [in] Box0Max - Max corner of the first box. +/// \param [in] Box1Min - Min corner of the second box. +/// \param [in] Box1Max - Max corner of the second box. +/// +/// \return true if the bounding boxes overlap, and false otherwise. +template +bool CheckBox2DBox2DOverlap(const Vector2& Box0Min, + const Vector2& Box0Max, + const Vector2& Box1Min, + const Vector2& Box1Max) +{ + VERIFY_EXPR(Box0Max.x >= Box0Min.x && Box0Max.y >= Box0Min.y && + Box1Max.x >= Box1Min.x && Box1Max.y >= Box1Min.y); + if (AllowTouch) + { + return !(Box0Min.x > Box1Max.x || Box1Min.x > Box0Max.x || Box0Min.y > Box1Max.y || Box1Min.y > Box0Max.y); + } + else + { + return !(Box0Min.x >= Box1Max.x || Box1Min.x >= Box0Max.x || Box0Min.y >= Box1Max.y || Box1Min.y >= Box0Max.y); + } +} + + } // namespace Diligent namespace std -- cgit v1.2.3