Add BoundingBox2 implementation

This commit is contained in:
Stefan Müller 2025-08-27 17:47:08 +02:00
parent 0aad311a32
commit 07dc369c2c

View File

@ -6,13 +6,19 @@ namespace BoundingBox
public class BoundingBox2 public class BoundingBox2
{ {
/// <summary>
/// Vector containing the lowest X and Y coordinates contained in the bounding box.
/// </summary>
public Vector2 Min; public Vector2 Min;
/// <summary>
/// Vector containing the highest X and Y coordinates contained in the bounding box.
/// </summary>
public Vector2 Max; public Vector2 Max;
public Vector2 Center => ; public Vector2 Center => (Max + Min) * 0.5f;
public Vector2 Size =>; public Vector2 Size => Max - Min;
public BoundingBox2(Vector2 min, Vector2 max) public BoundingBox2(Vector2 min, Vector2 max)
{ {
@ -22,21 +28,34 @@ namespace BoundingBox
public bool Contains(Vector2 position) public bool Contains(Vector2 position)
{ {
return Min.X <= position.X && position.X <= Max.X && Min.Y <= position.Y && position.Y <= Max.Y;
} }
public IntersectionType Intersects(BoundingBox2 other) public IntersectionType Intersects(BoundingBox2 other)
{ {
if (Min.X <= other.Min.X && other.Max.X <= Max.X && Min.Y <= other.Min.Y && other.Max.Y <= Max.Y)
{
return IntersectionType.Contains;
}
if ((other.Max.X <= Min.X || Max.X <= other.Min.X) && (other.Max.Y <= Min.Y || Max.Y <= other.Min.Y))
{
return IntersectionType.Disjoint;
}
return IntersectionType.Intersects;
} }
public void Translate(Vector2 translation) public void Translate(Vector2 translation)
{ {
Min += translation;
Max += translation;
} }
public void Expand(float expansion) public void Expand(float expansion)
{ {
Min.X -= expansion;
Min.Y -= expansion;
Max.X += expansion;
Max.Y += expansion;
} }
} }
} }