public void addPoint(QDPoint pt) { // If the current master node does not contain the point, keep increasing the square size until it does while (!masterNode.contains(pt)) { bool expandXPos = true; // Whether the quad tree should expand in positive or negative direction bool expandYPos = true; float newCnrX, newCnrY; // Check where new point is located relative to current master node if (pt.x > masterNode.maxX) { expandXPos = true; newCnrX = masterNode.minX; } else { expandXPos = false; newCnrX = masterNode.minX - masterNode.width; } if (pt.y > masterNode.maxY) { expandYPos = true; newCnrY = masterNode.minY; } else { expandYPos = false; newCnrY = masterNode.minY - masterNode.height; } // Initialise new master node QuadNode newMaster = new QuadNode(newCnrX, newCnrY, masterNode.size * 2f); newMaster.topL = new QuadNode(newCnrX, newCnrY, masterNode.size); newMaster.topR = new QuadNode(newCnrX + masterNode.size, newCnrY, masterNode.size); newMaster.botL = new QuadNode(newCnrX, newCnrY + masterNode.size, masterNode.size); newMaster.botR = new QuadNode(newCnrX + masterNode.size, newCnrY + masterNode.size, masterNode.size); // Insert current master node into newMaster if (expandXPos && expandYPos) { newMaster.topL = masterNode; } else if (expandXPos && !expandYPos) { newMaster.botL = masterNode; } else if (!expandXPos && expandYPos) { newMaster.topR = masterNode; } else { newMaster.botR = masterNode; } masterNode = newMaster; } masterNode.addPoint(pt); }
public bool addPoint(QDPoint pt) { // Perform the check that the QDPoint is actually inside the region if (!this.contains(pt)) { return(false); } // If we're at size and have no children create them if (pts.Count >= child_per_branch && topL == null) { subdivide(); pts.Add(pt); foreach (QDPoint thisPt in pts) { if (topL.addPoint(thisPt)) { continue; // Alternatively consider making the contains check a part of the addPoint function itself so it will return whether its possible to add or not } if (topR.addPoint(thisPt)) { continue; } if (botL.addPoint(thisPt)) { continue; } if (botR.addPoint(thisPt)) { continue; } } pts.Clear(); } // if we already have children, add it to them else if (topL != null) { if (topL.addPoint(pt)) { return(true); } if (topR.addPoint(pt)) { return(true); } if (botL.addPoint(pt)) { return(true); } if (botR.addPoint(pt)) { return(true); } } // otherwise add it to the current node list else { pts.Add(pt); } return(true); }