Advertisement
Guest User

Quadtreenose

a guest
Mar 30th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.77 KB | None | 0 0
  1. using ADS.Utility;
  2. using Engine;
  3. using Microsoft.Xna.Framework;
  4. using Microsoft.Xna.Framework.Graphics;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10.  
  11. namespace ADS.Managers.High_Tier.State
  12. {
  13.     class QuadtreeNode
  14.     {
  15.  
  16.         private Rectangle bounds;
  17.         public Rectangle Bounds { get { return bounds; } set { bounds = value; } }
  18.  
  19.         protected int maxItems;
  20.         private bool Split;
  21.  
  22.         private List<QuadtreeNode> children;
  23.         public List<QuadtreeNode> Children { get { return children; } set { children = value; } }
  24.  
  25.         private Quadtree tree;
  26.         public Quadtree Tree { get { return tree; } set { tree = value; } }
  27.  
  28.         private QuadtreeNode parent;
  29.         public QuadtreeNode Parent { get { return parent; } set { parent = value; } }
  30.  
  31.         public List<IEntity> items;
  32.  
  33.         public QuadtreeNode(QuadtreeNode _parent, int pMax, Quadtree pTree, Rectangle pBounds)
  34.         {
  35.             Parent = _parent;
  36.             maxItems = pMax;
  37.             Tree = pTree;
  38.             items = new List<IEntity>();
  39.             Bounds = pBounds;
  40.             Tree.Nodes.Add(this);
  41.         }
  42.  
  43.         public void Divide()
  44.         {
  45.             Children.Add(new QuadtreeNode(this, maxItems, Tree, new Rectangle(Bounds.Location.X, Bounds.Location.Y, Bounds.Width / 2, Bounds.Height / 2)));
  46.             Children.Add(new QuadtreeNode(this, maxItems, Tree, new Rectangle(Bounds.Location.X + (Bounds.Width / 2), Bounds.Location.Y, Bounds.Width / 2, Bounds.Height / 2)));
  47.             Children.Add(new QuadtreeNode(this, maxItems, Tree, new Rectangle(Bounds.Location.X, Bounds.Location.Y + (Bounds.Height / 2), Bounds.Width / 2, Bounds.Height / 2)));
  48.             Children.Add(new QuadtreeNode(this, maxItems, Tree, new Rectangle(Bounds.Location.X + (Bounds.Width / 2), Bounds.Location.Y + (Bounds.Height / 2), Bounds.Width / 2, Bounds.Height / 2)));
  49.             Split = true;
  50.         }
  51.  
  52.         public void insert(IEntity ent)
  53.         {
  54.             if (Bounds.Contains(ent.Bounds))
  55.             {
  56.                 items.Add(ent);
  57.  
  58.                 if (items.Count >= maxItems && !Split)
  59.                 {
  60.                     Divide();
  61.                     insertInChild(ent);
  62.                 }
  63.             }
  64.         }
  65.  
  66.         public void insertInChild(IEntity ent)
  67.         {
  68.             bool insert = false;
  69.             foreach (QuadtreeNode child in Children)
  70.             {
  71.                 if (ent.Bounds.Contains(child.Bounds) && insert == false)
  72.                 {
  73.                     child.insert(ent);
  74.                     insert = true;
  75.                 }
  76.             }
  77.         }
  78.  
  79.         public List<IEntity> getItems()
  80.         {
  81.             List<IEntity> ents = new List<IEntity>();
  82.  
  83.             if (Split)
  84.             {
  85.                 foreach (QuadtreeNode child in Children)
  86.                 {
  87.                     ents.AddRange(getItems());
  88.                 }
  89.             }
  90.  
  91.             foreach(IEntity ent in items)
  92.             {
  93.                 if (Bounds.Contains(ent.Bounds))
  94.                 {
  95.                     ents.Add(ent);
  96.                 }
  97.             }
  98.  
  99.             return ents;
  100.         }
  101.  
  102.         public void Collapse()
  103.         {
  104.             if (Split)
  105.             {
  106.                 if (items.Count < maxItems)
  107.                 {
  108.                     foreach (QuadtreeNode child in Children)
  109.                     {
  110.                         child.Collapse();
  111.                         child.clear();
  112.                         Children[Children.IndexOf(child)] = null;
  113.                     }
  114.                     Split = false;
  115.                 }
  116.             }
  117.         }
  118.  
  119.         public void clear()
  120.         {
  121.  
  122.             items.Clear();
  123.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement