implement a quadtree in csharp

Here's an implementation of a Quadtree in C#:

main.cs
public class Quadtree<T>
{
    private readonly int _maxObjectsPerNode;
    private readonly int _maxDepth;
    private readonly Rectangle _bounds;
    private int _depth;
    private List<T> _objects;
    private Quadtree<T>[] _nodes;

    public Quadtree(int maxObjectsPerNode, int maxDepth, Rectangle bounds, int depth = 0)
    {
        _maxObjectsPerNode = maxObjectsPerNode;
        _maxDepth = maxDepth;
        _bounds = bounds;
        _depth = depth;
        _objects = new List<T>();
        _nodes = null;
    }

    public void Insert(T item, Rectangle itemBounds)
    {
        if (!Bounds.Contains(itemBounds))
        {
            return;
        }

        if (_nodes != null)
        {
            foreach (var node in _nodes)
            {
                node.Insert(item, itemBounds);
            }

            return;
        }

        _objects.Add(item);

        if (_objects.Count > _maxObjectsPerNode && _depth < _maxDepth)
        {
            Split();

            foreach (var obj in _objects)
            {
                var objBounds = GetBounds(obj);
                foreach (var node in _nodes)
                {
                    node.Insert(obj, objBounds);
                }
            }

            _objects.Clear();
        }
    }

    public List<T> Query(Rectangle bounds)
    {
        var results = new List<T>();
        Query(bounds, results);
        return results;
    }

    private void Query(Rectangle queryBounds, List<T> results)
    {
        if (!Bounds.IntersectsWith(queryBounds))
        {
            return;
        }

        if (_nodes != null)
        {
            foreach (var node in _nodes)
            {
                node.Query(queryBounds, results);
            }
        }
        else
        {
            foreach (var obj in _objects)
            {
                if (queryBounds.Contains(GetBounds(obj)))
                {
                    results.Add(obj);
                }
            }
        }
    }

    private void Split()
    {
        var subWidth = (_bounds.Width / 2);
        var subHeight = (_bounds.Height / 2);
        var x = _bounds.X;
        var y = _bounds.Y;

        _nodes = new Quadtree<T>[4];
        _nodes[0] = new Quadtree<T>(_maxObjectsPerNode, _maxDepth, new Rectangle(x, y, subWidth, subHeight), _depth + 1);
        _nodes[1] = new Quadtree<T>(_maxObjectsPerNode, _maxDepth, new Rectangle(x + subWidth, y, subWidth, subHeight), _depth + 1);
        _nodes[2] = new Quadtree<T>(_maxObjectsPerNode, _maxDepth, new Rectangle(x, y + subHeight, subWidth, subHeight), _depth + 1);
        _nodes[3] = new Quadtree<T>(_maxObjectsPerNode, _maxDepth, new Rectangle(x + subWidth, y + subHeight, subWidth, subHeight), _depth + 1);
    }

    private Rectangle GetBounds(T item)
    {
        // Replace with your own logic to get the bounds of an item
        return default(Rectangle);
    }

    public Rectangle Bounds
    {
        get { return _bounds; }
    }
}
2982 chars
114 lines

You can use the Insert method to add objects to the quadtree and the Query method to retrieve a list of objects that intersect with a given bounds.

Note that you'll need to replace the GetBounds method with your own logic for getting the bounds of an object.

Here's an example usage:

main.cs
// Create a new quadtree that can hold up to 10 objects per node and has a max depth of 5
var quadtree = new Quadtree<GameObject>(10, 5, new Rectangle(0, 0, 100, 100));

// Insert some objects
var obj1Bounds = new Rectangle(20, 20, 10, 10);
var obj1 = new GameObject();
quadtree.Insert(obj1, obj1Bounds);

var obj2Bounds = new Rectangle(50, 50, 10, 10);
var obj2 = new GameObject();
quadtree.Insert(obj2, obj2Bounds);

// Query for objects that intersect with a given bounds
var queryBounds = new Rectangle(0, 0, 30, 30);
var results = quadtree.Query(queryBounds);
565 chars
16 lines

gistlibby LogSnag