From af8856188d4ff2e62d2c1cf0e9a190fd7bc19920 Mon Sep 17 00:00:00 2001 From: Nejcraft Date: Fri, 16 Jul 2021 04:32:47 +0200 Subject: [PATCH 1/2] 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). From 52ba7a7a08926d27791f3ec30f9923539d80a436 Mon Sep 17 00:00:00 2001 From: Nejcraft Date: Fri, 16 Jul 2021 11:40:57 +0200 Subject: [PATCH 2/2] I shouldn't code at 4 in the morning :D --- Source/Engine/Core/Math/Rectangle.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Source/Engine/Core/Math/Rectangle.cs b/Source/Engine/Core/Math/Rectangle.cs index 885cf6d37..bb085ef6a 100644 --- a/Source/Engine/Core/Math/Rectangle.cs +++ b/Source/Engine/Core/Math/Rectangle.cs @@ -288,12 +288,12 @@ namespace FlaxEngine /// /// Computed nearest distance between 2 rectangles. /// - /// Firat rectangle + /// 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; + return Vector2.Max(Vector2.Zero, Vector2.Abs(a.Center - b.Center) - ((a.Size + b.Size) * 0.5f)).Length; } ///