Ejemplo n.º 1
0
        public void AddItem(IQuadItem toAdd)
        {
            int px, py;
            px = (int)(toAdd.Position.X / RegionWidth);
            py = (int)(toAdd.Position.Y / RegionHeight);

            //add super regions if they are needed.
            //TODO: clean
            if (!HaveRegion(new Rectangle(px * RegionWidth + 1, py * RegionHeight + 1, 1, 1)))
            {
                Leaves.Add(new QuadTreeLeaf(px * RegionWidth, py * RegionHeight, RegionWidth, RegionHeight, this));
            }
            if (!HaveRegion(new Rectangle((px + 1) * RegionWidth + 1, py * RegionHeight + 1, 1, 1)))
            {
                Leaves.Add(new QuadTreeLeaf((px + 1) * RegionWidth, py * RegionHeight, RegionWidth, RegionHeight, this));
            }
            if (!HaveRegion(new Rectangle(px * RegionWidth + 1, (py + 1) * RegionHeight + 1, 1, 1)))
            {
                Leaves.Add(new QuadTreeLeaf(px * RegionWidth, (py + 1) * RegionHeight, RegionWidth, RegionHeight, this));
            }
            if (!HaveRegion(new Rectangle((px + 1) * RegionWidth + 1, (py + 1) * RegionHeight + 1, 1, 1)))
            {
                Leaves.Add(new QuadTreeLeaf((px + 1) * RegionWidth, (py + 1) * RegionHeight, RegionWidth, RegionHeight, this));
            }

            for (int i = 0; i < Leaves.Count; i++)
            {
                if (Leaves[i].Region.Intersects(toAdd.Position))
                {
                    Leaves[i].AddItem(toAdd);
                }
            }
        }
Ejemplo n.º 2
0
 public void DelItem(IQuadItem item)
 {
     for (int i = 0; i < Leaves.Count; i++)
     {
         if (Leaves[i].Region.Intersects(item.Position))
         {
             Leaves[i].DelItem(item);
         }
     }
 }
Ejemplo n.º 3
0
 public void DelItem(IQuadItem item)
 {
     if (!Items.Contains(item))
     {
         for (int i = 0; i < Children.Count(); i++)
         {
             if (Children[i].Region.Intersects(item.Position))
             {
                 Children[i].DelItem(item);
             }
         }
     }
     else
     {
         Items.Remove(item);
     }
 }
Ejemplo n.º 4
0
        public void AddItem(IQuadItem toAdd)
        {
            //if the item isnt in the super region it isnt in the child regions either
            if (toAdd.Position.Intersects(Region))
            {
                if (Children[0] == null)
                {
                    //if (!Items.Contains(toAdd))
                    Items.Add(toAdd);
                }
                else
                {
                    for (int i = 0; i < 4; i++)
                        Children[i].AddItem(toAdd);
                }
            }

            //if this leaf gets too big, we split it up
            if ((Count > Parent.ChildSize) && (Width > Parent.RegionWidthMinimum))
            {
                for (int i = 0; i < 4; i++)
                {
                    MakeSplit(i);
                    Children[i] = new QuadTreeLeaf(split_rect.X, split_rect.Y, split_rect.Width, split_rect.Height, this.Parent);
                }
                for (int i = 0; i < Count; i++)
                {
                    for (int j = 0; j < 4; j++)
                    {
                        Children[j].AddItem(Items[i]);
                    }
                }
                Items.Clear();
            }
        }