Implement missing origin calculations.

This commit is contained in:
2026-09-07 22:02:08 +03:00
parent 293ef241f1
commit df8aeca1db
+36 -12
View File
@@ -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)
{