public void AddNode(QuadNode node) { if (_id * 4 + 1 == node._id) { _nodeRT = node; return; } if (_id * 4 + 2 == node._id) { _nodeLT = node; return; } if (_id * 4 + 3 == node._id) { _nodeLB = node; return; } if (_id * 4 + 4 == node._id) { _nodeRB = node; return; } if (_nodeRT != null) _nodeRT.AddNode(node); if (_nodeRT != null) _nodeLT.AddNode(node); if (_nodeRB != null) _nodeRB.AddNode(node); if (_nodeLB != null) _nodeLB.AddNode(node); }
public void SetAllData(List<Item> allObjects, Rectangle mapPartition) { if (allObjects == null) throw new Exception("List objects is null!"); QuadNode root = new QuadNode(0, mapPartition); _tree.Root = root; SetQuadTree(allObjects, _tree.Root); }
public QuadNode(int id, Rectangle partition) { _id = id; _partition = partition; _nodeLT = null; _nodeRT = null; _nodeLB = null; _nodeRB = null; _objects = new List<Item>(); }
public void SetQuadTree(List<Item> allObjects, QuadNode root) { int widthLeft, heightTop, widthRight, heightBottom; QuadNode nodeRT, nodeLT, nodeLB, nodeRB; if (root.Partition.Width > _screenWidth) { widthLeft = root.Partition.Width / 2; widthRight = root.Partition.Width - widthLeft; } else { widthLeft = root.Partition.Width; widthRight = 0; } if(root.Partition.Height > _screenHeight) { heightTop = root.Partition.Height / 2; heightBottom = root.Partition.Height - heightTop; } else { heightTop = root.Partition.Height; heightBottom = 0; } nodeLT = new QuadNode((root.Id * 4 + 2), new Rectangle(root.Partition.X, root.Partition.Y, widthLeft, heightTop)); nodeLB = new QuadNode((root.Id * 4 + 3), new Rectangle(root.Partition.X, root.Partition.Y+heightTop, widthLeft, heightBottom)); nodeRB = new QuadNode((root.Id * 4 + 4), new Rectangle(root.Partition.X+widthLeft, root.Partition.Y+heightTop, widthRight, heightBottom)); nodeRT = new QuadNode((root.Id * 4 + 1), new Rectangle(root.Partition.X + widthLeft, root.Partition.Y, widthRight, heightTop)); root.NodeLT = nodeLT; root.NodeLB = nodeLB; root.NodeRB = nodeRB; root.NodeRT = nodeRT; if(nodeLT.Partition.Width <= _screenWidth && nodeLT.Partition.Height <= _screenHeight) { for(int i=0;i<allObjects.Count;i++) { if(allObjects[i].ItemRectangle.IntersectsWith(nodeLT.Partition)) { nodeLT.AddObject(allObjects[i]); allObjects.RemoveAt(i--); } } } else { SetQuadTree(allObjects, nodeLT); } if (nodeLB.Partition.Width <= _screenWidth && nodeLB.Partition.Height <= _screenHeight) { for (int i = 0; i < allObjects.Count; i++) { if (allObjects[i].ItemRectangle.IntersectsWith(nodeLB.Partition)) { nodeLB.AddObject(allObjects[i]); allObjects.RemoveAt(i--); } } } else { SetQuadTree(allObjects, nodeLB); } if (nodeRB.Partition.Width <= _screenWidth && nodeRB.Partition.Height <= _screenHeight) { for (int i = 0; i < allObjects.Count; i++) { if (allObjects[i].ItemRectangle.IntersectsWith(nodeRB.Partition)) { nodeRB.AddObject(allObjects[i]); allObjects.RemoveAt(i--); } } } else { SetQuadTree(allObjects, nodeRB); } if (nodeRT.Partition.Width <= _screenWidth && nodeRT.Partition.Height <= _screenHeight) { for (int i = 0; i < allObjects.Count; i++) { if (allObjects[i].ItemRectangle.IntersectsWith(nodeRT.Partition)) { nodeRT.AddObject(allObjects[i]); allObjects.RemoveAt(i--); } } } else { SetQuadTree(allObjects, nodeRT); } }