Beispiel #1
0
        public SpatialEntry <T> Add(T value, AABB bounds)
        {
            var entry = new SpatialEntry <T> {
                Value = value, Bounds = bounds
            };

            foreach (var cell in Cells(bounds, true))
            {
                cell.Add(entry);
            }
            return(entry);
        }
Beispiel #2
0
        public void Update(ref SpatialEntry <T> entry, AABB bounds)
        {
            if (entry.Bounds.Similar(bounds))
            {
                return;
            }
            var o = Indices(entry.Bounds);
            var n = Indices(bounds);

            entry.Bounds = bounds;

            for (var x = o.X; x < o.Right; x++)
            {
                for (var y = o.Y; y < o.Bottom; y++)
                {
                    if (x < n.X || x >= n.Right || y < n.Y || y >= n.Bottom)
                    {
                        var pt = new Point(x, y);
                        if (Backing.TryGetValue(pt, out var list))
                        {
                            list.Remove(entry);
                            if (list.Count == 0)
                            {
                                Backing.Remove(pt);
                            }
                        }
                    }
                }
            }

            for (var x = n.X; x < n.Right; x++)
            {
                for (var y = n.Y; y < n.Bottom; y++)
                {
                    var list = Backing.GetOrCreate(new Point(x, y));
                    for (var i = 0; i < list.Count; i++)
                    {
                        if (list[i].Value.Equals(entry.Value))
                        {
                            list[i] = entry;
                            goto cont;
                        }
                    }
                    list.Add(entry);
                    cont :;
                }
            }
        }
Beispiel #3
0
        public void Remove(SpatialEntry <T> entry)
        {
            var indices = Indices(entry.Bounds);

            for (var x = 0; x < indices.Width; x++)
            {
                for (var y = 0; y < indices.Height; y++)
                {
                    var pt = new Point(indices.X + x, indices.Y + y);
                    if (Backing.TryGetValue(pt, out var cell))
                    {
                        cell.Remove(entry);
                        if (cell.Count == 0)
                        {
                            Backing.Remove(pt);
                        }
                    }
                }
            }
        }
Beispiel #4
0
 public bool Equals(SpatialEntry <T> other) =>
 Value.Equals(other.Value);