From af8856188d4ff2e62d2c1cf0e9a190fd7bc19920 Mon Sep 17 00:00:00 2001 From: Nejcraft Date: Fri, 16 Jul 2021 04:32:47 +0200 Subject: [PATCH] Rectangle distance Will be usefull for my UI nav --- Source/Engine/Core/Math/Rectangle.cs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/Source/Engine/Core/Math/Rectangle.cs b/Source/Engine/Core/Math/Rectangle.cs index d1fb16abc..885cf6d37 100644 --- a/Source/Engine/Core/Math/Rectangle.cs +++ b/Source/Engine/Core/Math/Rectangle.cs @@ -284,6 +284,17 @@ namespace FlaxEngine Vector2 toExpand = Size * (scale - 1.0f) * 0.5f; return new Rectangle(Location - toExpand * 0.5f, Size + toExpand); } + + /// + /// Computed nearest distance between 2 rectangles. + /// + /// Firat rectangle + /// Second rectangle + /// Resulting distance, 0 if overlapping + public static float Distance(Rectangle a, Rectangle b) + { + return Vector2.Max(Vector2.Zero, Vector2.Abs(a.Center - b.Center) - (a.Size * 0.5f - b.Size * 0.5f)).Length; + } /// /// Calculates a rectangle that includes the margins (inside).