Ejemplo n.º 1
0
 public QNode(int level, Rectangle bound, QNode parent)
 {
     this._level  = level;
     this._bound  = bound;
     this._parent = parent;
     this.Id      = (parent == null) ? 0: parent.Id;
     _listObject  = new List <GameObject>();
     _childs      = new QNode[4];
 }
Ejemplo n.º 2
0
Archivo: QNode.cs Proyecto: 7ung/NMGAME
 public QNode(int level, Rectangle bound, QNode parent)
 {
     this._level = level;
     this._bound = bound;
     this._parent = parent;
     this.Id = (parent == null) ? 0: parent.Id;
     _listObject = new List<GameObject>();
     _childs = new QNode[4];
 }
Ejemplo n.º 3
0
        // Helper -> dont care
        public static void GenerateIds(QNode parent)
        {
            parent.SW.Id = parent.Id;
            int offset = 2 << parent.Level;

            parent.SE.Id = Convert.ToInt64(1 << offset) | parent.Id;
            parent.NE.Id = Convert.ToInt64(1 << (offset + 1)) | parent.Id;
            parent.NW.Id = parent.SE.Id | parent.NE.Id;
        }
Ejemplo n.º 4
0
        // IMPORTANT
        // Tạo node con từ các object vào bound.
        public void initChild()
        {
            // Chỉ tạo node con khi thoả hai điều kiện là số bậc không quá cao, và sô object không quá ít.
            if (this.Level >= MainForm.Settings.MaxLevelQuadTree || this.ListObject.Count <= MainForm.Settings.MaxObjectQuadTree)
            {
                return;
            }

            // Tính 4 hình chữ nhật con của node này
            Rectangle[] rects = this.devideBound();

            // Khởi tạo đối tương childs
            for (int i = 0; i < 4; i++)
            {
                this.Childs[i] = new QNode(this.Level + 1, rects[i], this);
            }

            // Tính id cho node. nhưng gần như tôi không dùng đến id. Có thể dont care.
            QNode.GenerateIds(this);

            for (int i = 0; i < 4; i++)
            {
                foreach (GameObject gameobj in this.ListObject)
                {
                    // done = true nếu gameobject nằm trong child[i]
                    bool done = this.Childs[i].insertObject(gameobj);
                }
            }

            // Node không phải lá không được giữ lại object.
            this.removeall();

            // Lặp lại quá trình trên cho tát cả childs của nó
            for (int i = 0; i < 4; i++)
            {
                Childs[i].initChild();
            }
        }
Ejemplo n.º 5
0
Archivo: QNode.cs Proyecto: 7ung/NMGAME
 // Helper -> dont care
 public static void GenerateIds(QNode parent)
 {
     parent.SW.Id = parent.Id;
     int offset = 2 << parent.Level;
     parent.SE.Id = Convert.ToInt64(1 << offset)| parent.Id;
     parent.NE.Id = Convert.ToInt64(1 << (offset + 1)) | parent.Id;
     parent.NW.Id = parent.SE.Id | parent.NE.Id;
 }
Ejemplo n.º 6
0
 private void drawQuadTreeNode(QNode node, Graphics graphics)
 {
     if (node == null)
         return;
     if (node.isLeaf() == true)
     {
         if (node.ListObject.Any())
             graphics.FillRectangle(brush_quadnode, node.Bound);
         else
             graphics.FillRectangle(brush_quadnode_noobject, node.Bound);
         graphics.DrawRectangle(new Pen(new SolidBrush(Color.FromArgb(255, 45, 45, 45))), node.Bound);
     }
     else
     {
         for (int i = 0; i < 4; i++)
         {
             drawQuadTreeNode(node.Childs[i], graphics);
         }
     }
 }
Ejemplo n.º 7
0
 private static void Save(XmlTextWriter writter, QNode qnode, string path)
 {
     if (qnode == null)
         return;
     writter.WriteStartElement("QNode");
     {
         writter.WriteAttributeString("Id", qnode.Id.ToString());
         writter.WriteAttributeString("Level", qnode.Level.ToString());
         writter.WriteAttributeString("X", qnode.Bound.X.ToString());
         writter.WriteAttributeString("Y", qnode.Bound.Y.ToString());
         writter.WriteAttributeString("Width", qnode.Bound.Width.ToString());
         writter.WriteAttributeString("Height", qnode.Bound.Height.ToString());
         if (qnode.isLeaf() == true && qnode.ListObject.Any())
         {
             string str = String.Empty;
             foreach (var obj in qnode.ListObject)
             {
                 str += obj.Name + " ";
             }
             writter.WriteStartElement("Objects");
             writter.WriteString(str);
             writter.WriteEndElement();
         }
         else
         {
             for (int i = 0; i < 4; i++)
             {
                 Save(writter, qnode.Childs[i], path);
             }
         }
     }
     writter.WriteEndElement();
 }
Ejemplo n.º 8
0
        public void InitQuadTree(int level, Rectangle bound)
        {
            // Bình thường có thể lấy bound của root bằng với kích thước map.
            // Ta có thể option cho cái bound là một hình vuông với cạnh là max của width và height của map. để tránh trường hợp map theo chiều dài hoặc chiều rộng.

            int edge = Math.Max(bound.Width, bound.Height);
            bound.Size = new Size(edge, edge);

            Thread thread = new Thread(new ThreadStart(() =>
            {

                this.QuadTree = new QuadTree.QNode(0, bound, null);
                this.QuadTree.ListObject = this.ListItem.ToList();
                this.QuadTree.initChild();
            }));

            thread.Start();
        }
Ejemplo n.º 9
0
 public static void SaveQuadTree( QNode root, string path)
 {
     using (XmlTextWriter writter = new XmlTextWriter(path, Encoding.UTF8))
     {
         writter.Formatting = Formatting.Indented;
         writter.WriteStartDocument();
         Save(writter, root, path);
         writter.WriteEndDocument();
     }
 }