Add MaxDimension property to Box2 (#4871)

* Add MaxDimension property to Box2

Sometimes I want to pretend it's a circle radius.

* a

---------

Co-authored-by: Pieter-Jan Briers <pieterjan.briers+git@gmail.com>
This commit is contained in:
metalgearsloth
2024-01-30 23:28:05 +11:00
committed by GitHub
parent 754d5a1fbb
commit 5057ff97a3
2 changed files with 31 additions and 21 deletions

View File

@@ -39,6 +39,7 @@ END TEMPLATE-->
### New features
* Add MaxDimension property to Box2 to return the higher of the Width or Height.
* Add GetLocalPosition to convert ScreenCoordinates to coordinates relative to the control. Ignores window.
* Add GlobalRect and GlobalPixelRect for controls to get their UIBox2i in screen terms.
* Add dotted line drawing to DrawingHandleScreen.

View File

@@ -75,6 +75,15 @@ namespace Robust.Shared.Maths
get => new(Width, Height);
}
/// <summary>
/// Returns the highest of width or height.
/// </summary>
public readonly float MaxDimension
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => MathF.Max(Height, Width);
}
public readonly Vector2 Center
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
@@ -177,13 +186,13 @@ namespace Robust.Shared.Maths
[Pure]
public readonly Box2 Intersect(in Box2 other)
{
var ourLeftBottom = new System.Numerics.Vector2(Left, Bottom);
var ourRightTop = new System.Numerics.Vector2(Right, Top);
var otherLeftBottom = new System.Numerics.Vector2(other.Left, other.Bottom);
var otherRightTop = new System.Numerics.Vector2(other.Right, other.Top);
var ourLeftBottom = new Vector2(Left, Bottom);
var ourRightTop = new Vector2(Right, Top);
var otherLeftBottom = new Vector2(other.Left, other.Bottom);
var otherRightTop = new Vector2(other.Right, other.Top);
var max = System.Numerics.Vector2.Max(ourLeftBottom, otherLeftBottom);
var min = System.Numerics.Vector2.Min(ourRightTop, otherRightTop);
var max = Vector2.Max(ourLeftBottom, otherLeftBottom);
var min = Vector2.Min(ourRightTop, otherRightTop);
if (max.X <= min.X && max.Y <= min.Y)
return new Box2(max.X, max.Y, min.X, min.Y);
@@ -210,13 +219,13 @@ namespace Robust.Shared.Maths
[Pure]
public readonly Box2 Union(in Box2 other)
{
var ourLeftBottom = new System.Numerics.Vector2(Left, Bottom);
var otherLeftBottom = new System.Numerics.Vector2(other.Left, other.Bottom);
var ourRightTop = new System.Numerics.Vector2(Right, Top);
var otherRightTop = new System.Numerics.Vector2(other.Right, other.Top);
var ourLeftBottom = new Vector2(Left, Bottom);
var otherLeftBottom = new Vector2(other.Left, other.Bottom);
var ourRightTop = new Vector2(Right, Top);
var otherRightTop = new Vector2(other.Right, other.Top);
var leftBottom = System.Numerics.Vector2.Min(ourLeftBottom, otherLeftBottom);
var rightTop = System.Numerics.Vector2.Max(ourRightTop, otherRightTop);
var leftBottom = Vector2.Min(ourLeftBottom, otherLeftBottom);
var rightTop = Vector2.Max(ourRightTop, otherRightTop);
if (leftBottom.X <= rightTop.X && leftBottom.Y <= rightTop.Y)
return new Box2(leftBottom.X, leftBottom.Y, rightTop.X, rightTop.Y);
@@ -396,11 +405,11 @@ namespace Robust.Shared.Maths
[Pure]
public static Box2 Union(in Vector2 a, in Vector2 b)
{
var vecA = new System.Numerics.Vector2(a.X, a.Y);
var vecB = new System.Numerics.Vector2(b.X, b.Y);
var vecA = new Vector2(a.X, a.Y);
var vecB = new Vector2(b.X, b.Y);
var min = System.Numerics.Vector2.Min(vecA, vecB);
var max = System.Numerics.Vector2.Max(vecA, vecB);
var min = Vector2.Min(vecA, vecB);
var max = Vector2.Max(vecA, vecB);
return new Box2(min.X, min.Y, max.X, max.Y);
}
@@ -412,12 +421,12 @@ namespace Robust.Shared.Maths
[Pure]
public readonly Box2 ExtendToContain(Vector2 vec)
{
var leftBottom = new System.Numerics.Vector2(Left, Bottom);
var rightTop = new System.Numerics.Vector2(Right, Top);
var vector = new System.Numerics.Vector2(vec.X, vec.Y);
var leftBottom = new Vector2(Left, Bottom);
var rightTop = new Vector2(Right, Top);
var vector = new Vector2(vec.X, vec.Y);
var min = System.Numerics.Vector2.Min(vector, leftBottom);
var max = System.Numerics.Vector2.Max(vector, rightTop);
var min = Vector2.Min(vector, leftBottom);
var max = Vector2.Max(vector, rightTop);
return new Box2(min.X, min.Y, max.X, max.Y);
}