Add BoundingBox2.Include() from Quadtree.IncludeInBoundingBox()

This commit is contained in:
Stefan Müller 2025-08-27 17:59:56 +02:00
parent e84746fab0
commit 504ec41ebc
2 changed files with 21 additions and 25 deletions

View File

@ -57,5 +57,25 @@ namespace Quadtree
Max.X += expansion;
Max.Y += expansion;
}
public void Include(Vector2 position)
{
if (Min.X > position.X)
{
Min.X = position.X;
}
else if (Max.X < position.X)
{
Max.X = position.X;
}
if (Min.Y > position.Y)
{
Min.Y = position.Y;
}
else if (Max.Y < position.Y)
{
Max.Y = position.Y;
}
}
}
}

View File

@ -234,7 +234,7 @@ namespace Quadtree
item.Next = _vertices[0].FirstChildIndex;
_items[itemIndex] = item;
_vertices[0] = new QuadtreeVertex(itemIndex, _vertices[0].ChildCount + 1);
IncludeInBoundingBox(ref _rootBoundingBox, position);
_rootBoundingBox.Include(position);
}
}
@ -416,29 +416,5 @@ namespace Quadtree
}
}
}
/// <summary>
/// Slightly more efficient implementation of BoundingBox.Include(), assuming that Min.X <= Max.X,
/// Min.Z <= Max.Z, and all Y = 0.
/// </summary>
private void IncludeInBoundingBox(ref BoundingBox2 boundingBox, Vector2 position)
{
if (boundingBox.Min.X > position.X)
{
boundingBox.Min.X = position.X;
}
else if (boundingBox.Max.X < position.X)
{
boundingBox.Max.X = position.X;
}
if (boundingBox.Min.Y > position.Y)
{
boundingBox.Min.Y = position.Y;
}
else if (boundingBox.Max.Y < position.Y)
{
boundingBox.Max.Y = position.Y;
}
}
}
}