Ejemplo n.º 1
0
    def add(self, pt):
        """Add pt to the QuadNode, if not already present."""
        # Doesn't fit in this node (sanity check: not truly needed since tree checks)
        if not containsPoint(self.region, pt):
            return False

        node = self
        while node:
            # if we have points, then we are leaf node. Check here
            if node.points != None:
                if pt in node.points:
                    return False

                # Add if room                
                if len(node.points) < 4:
                    node.points.append(pt)
                    return True
                else:
                    node.subdivide()
            
            # Find quadrant into which to add
            quad = node.quadrant(pt)
            if node.children[quad] is None:
                node.children[quad] = node.subquadrant(quad)
            node = node.children[quad]
Ejemplo n.º 2
0
 def remove(self, pt):
     """Remove pt from tree. Return True if was removed, else False."""
     if self.root is None:
         return False
     
     if not containsPoint(self.region, pt):
         return False
     
     self.root,updated = self.root.remove(pt)
     return updated
Ejemplo n.º 3
0
 def add(self, pt):
     """Add point to QuadTree. Return False if outside region or already exists."""
     # Doesn't belong in this region, leave now
     if not containsPoint(self.region, pt):
         return False
     
     if self.root is None:
         self.root = QuadNode(self.region)
         
     return self.root.add(pt)
Ejemplo n.º 4
0
 def remove(self, pt):
     """Remove pt should it exist in tree."""
     if self.root is None:
         return False
     
     if not containsPoint(self.region, pt):
         return False
     
     self.root,updated = self.root.remove(pt)
     return updated
Ejemplo n.º 5
0
    def add(self, pt):
        """Add point to QuadTree."""
        # Not able to fit in this tree
        if not containsPoint(self.region, pt):
            return False

        if self.root is None:
            self.root = QuadNode(self.region, pt)
            return True
        
        return self.root.add(pt)
Ejemplo n.º 6
0
    def __contains__(self, pt):
        """Check whether exact point appears in QuadTree."""
        if not containsPoint(self.region, pt):
            return False
        
        node = self.root
        while node:
            if node.full:
                return True

            quad = node.quadrant(pt)
            node = node.children[quad]
    
        return False