Esempio n. 1
0
        public override string ToString()
        {
            var white = WhiteNode.ToString() ?? EmptyNodeIndicator;
            var black = BlackNode.ToString() ?? EmptyNodeIndicator;

            return($"<{white} {black}>");
        }
Esempio n. 2
0
 public Node Remove(T oldElement)
 {
     if (this.data.Equals(oldElement))
     {
         return(WhiteNode.GetSingleton());
     }
     return(this);
 }
Esempio n. 3
0
            public Node Remove(T oldElement)
            {
                float positionX = oldElement.GetPositionX();
                float positionY = oldElement.GetPositionY();

                if (!myRegion.contains(oldElement))
                {
                    if (isRootNode)
                    {
                        return(this);
                    }
                    throw new System.Exception("Element does not belong in this Grey Node");
                }

                for (int i = 0; i < nodes.Length; i++)
                {
                    if (childRegions[i].contains(oldElement))
                    {
                        nodes[i] = nodes[i].Remove(oldElement);
                    }
                }

                if (isRootNode)
                {
                    return(this);
                }

                int  countBlackNodes = 0;
                int  countGreyNodes  = 0;
                Node lastBlackNode   = null;

                foreach (Node node in nodes)
                {
                    if (node is GreyNode)
                    {
                        countGreyNodes++;
                        return(this);
                    }
                    else if (node is BlackNode)
                    {
                        countBlackNodes++;
                        lastBlackNode = node;
                    }
                }

                if (countBlackNodes == 0 && countGreyNodes == 0 && !isRootNode)
                {
                    return(WhiteNode.GetSingleton());
                }
                else if (countBlackNodes == 1 && countGreyNodes == 0 && lastBlackNode != null && !isRootNode)
                {
                    return(lastBlackNode);
                }

                return(this);
            }
Esempio n. 4
0
            public GreyNode(Region region, bool rootNode = false)
            {
                if (region.width < 0 || region.height < 0)
                {
                    throw new System.Exception("Invalid boundaries in GreyNode");
                }
                Node whiteSingleton = WhiteNode.GetSingleton();

                nodes = new Node[4] {
                    whiteSingleton, whiteSingleton, whiteSingleton, whiteSingleton
                };
                this.isRootNode = rootNode;
                myRegion        = region;
                childRegions    = region.generateSubregions();
            }
Esempio n. 5
0
    /**
     * Create an image which is represented using a QuadTreeNode.
     *
     * @param size      size of image
     * @param center_x  x coordinate of center
     * @param center_y; y coordinate of center
     * @param parent    parent quad tree node
     * @param quadrant  the quadrant that the sub tree is in
     * @param level     the level of the tree
     */
    public static QuadTreeNode createTree(int size, int center_x, int center_y, QuadTreeNode parent, Quadrant quadrant, int level)
    {
        QuadTreeNode node;

        int intersect = checkIntersect(center_x, center_y, size);

        size = size / 2;
        if (intersect == 0 && size < 512)
        {
            node = new WhiteNode(quadrant, parent);
        }
        else if (intersect == 2)
        {
            node = new BlackNode(quadrant, parent);
        }
        else
        {
            if (level == 0)
            {
                node = new BlackNode(quadrant, parent);
            }
            else
            {
                node = new GreyNode(quadrant, parent);
                QuadTreeNode sw = createTree(size, center_x - size, center_y - size, node,
                                             Quadrant.cSouthWest, level - 1);
                QuadTreeNode se = createTree(size, center_x + size, center_y - size, node,
                                             Quadrant.cSouthEast, level - 1);
                QuadTreeNode ne = createTree(size, center_x + size, center_y + size, node,
                                             Quadrant.cNorthEast, level - 1);
                QuadTreeNode nw = createTree(size, center_x - size, center_y + size, node,
                                             Quadrant.cNorthWest, level - 1);
                node.setChildren(nw, ne, sw, se);
            }
        }
        return(node);
    }
Esempio n. 6
0
	/**
	 * Create an image which is represented using a QuadTreeNode.
	 *
	 * @param size      size of image
	 * @param center_x  x coordinate of center
	 * @param center_y; y coordinate of center
	 * @param parent    parent quad tree node
	 * @param quadrant  the quadrant that the sub tree is in
	 * @param level     the level of the tree
	 */
	public static QuadTreeNode createTree(int size, int center_x, int center_y, QuadTreeNode parent, Quadrant quadrant, int level)
	{
		QuadTreeNode node;

		int intersect = checkIntersect(center_x, center_y, size);
		size = size / 2;
		if(intersect == 0 && size < 512)
		{
			node = new WhiteNode(quadrant, parent);
		}
		else if(intersect == 2)
		{
			node = new BlackNode(quadrant, parent);
		}
		else
		{
			if(level == 0)
			{
				node = new BlackNode(quadrant, parent);
			}
			else
			{
				node = new GreyNode(quadrant, parent);
				QuadTreeNode sw = createTree(size, center_x - size, center_y - size, node,
					Quadrant.cSouthWest, level - 1);
				QuadTreeNode se = createTree(size, center_x + size, center_y - size, node,
					Quadrant.cSouthEast, level - 1);
				QuadTreeNode ne = createTree(size, center_x + size, center_y + size, node,
					Quadrant.cNorthEast, level - 1);
				QuadTreeNode nw = createTree(size, center_x - size, center_y + size, node,
					Quadrant.cNorthWest, level - 1);
				node.setChildren(nw, ne, sw, se);
			}
		}
		return node;
	}