diff --git a/Dashboard.Common/Box2d.cs b/Dashboard.Common/Box2d.cs index 0904525..2809eeb 100644 --- a/Dashboard.Common/Box2d.cs +++ b/Dashboard.Common/Box2d.cs @@ -25,19 +25,43 @@ namespace Dashboard { } - public static Box2d FromPositionAndSize(Vector2 position, Vector2 size, Origin anchor = Origin.Center) - { - Vector2 half = size * 0.5f; - switch (anchor) + public static Box2d FromPositionAndSize(Vector2 position, Vector2 size, Origin anchor = Origin.Center) => + // Hoping the redundent additions+muls become conditional fma instructions. + anchor switch { - case Origin.Center: - return new Box2d(position - half, position + half); - case Origin.TopLeft: - return new Box2d(position, position + size); - default: - throw new NotImplementedException(); - } - } + Origin.Left => new Box2d( + position + (size * new Vector2(0.0f, -0.5f)), + position + (size * new Vector2(1.0f, 0.5f)) + ), + Origin.TopLeft => new Box2d(position, position + size), + Origin.Top => new Box2d( + position + (size * new Vector2(-0.5f, 0f)), + position + (size * new Vector2(0.5f, 1f))), + Origin.TopRight => new Box2d( + position + (size * new Vector2(-1.0f, 0.0f)), + position + (size * new Vector2(0.0f, 1.0f)) + ), + Origin.Right => new Box2d( + position + (size * new Vector2(-1.0f, -0.5f)), + position + (size * new Vector2(0.0f, 0.5f)) + ), + Origin.BottomRight => new Box2d( + position + (size * new Vector2(-1.0f, -1.0f)), + position + (size * new Vector2(0.0f, 0.0f)) + ), + Origin.Bottom => new Box2d( + position + (size * new Vector2(-0.5f, -1.0f)), + position + (size * new Vector2(0.5f, 0.0f)) + ), + Origin.BottomLeft => new Box2d( + position + (size * new Vector2(0.0f, -1.0f)), + position + (size * new Vector2(1.0f, 0.0f)) + ), + Origin.Center => new Box2d( + position + (size * new Vector2(-0.5f, -0.5f)), + position + (size * new Vector2(0.5f, 0.5f))), + _ => throw new ArgumentOutOfRangeException(nameof(anchor)), + }; public static Box2d Union(Box2d left, Box2d right) {