Example #1
0
            public Node Insert(Rectangle rectangle)
            {
                // if this is not a leaf
                if (this.left != null)
                {
                    Node nodeLeft = this.left.Insert(rectangle);
                    if (nodeLeft != null)
                    {
                        return(nodeLeft);
                    }

                    return(this.right.Insert(rectangle));
                }

                // already a rectangle in this node
                if (this.filled)
                {
                    return(null);
                }

                // rectangle does not fit in bounds
                if (!rectangle.FitsIn(this.Rectangle))
                {
                    return(null);
                }

                // perfectly snug fit
                if (rectangle.Compare(this.Rectangle) == 0)
                {
                    this.filled = true;
                    return(this);
                }

                // split this node into two children
                this.left  = new Node();
                this.right = new Node();

                int diffWidth  = this.Rectangle.Width - rectangle.Width;
                int diffHeight = this.Rectangle.Height - rectangle.Height;

                // either split horizontally or vertically
                if (diffWidth > diffHeight)
                {
                    this.left.Rectangle  = new Rectangle(this.Rectangle.X, this.Rectangle.Y, rectangle.Width, this.Rectangle.Height);
                    this.right.Rectangle = new Rectangle(this.Rectangle.X + rectangle.Width, this.Rectangle.Y, this.Rectangle.Width - rectangle.Width, this.Rectangle.Height);
                }
                else
                {
                    this.left.Rectangle  = new Rectangle(this.Rectangle.X, this.Rectangle.Y, this.Rectangle.Width, rectangle.Height);
                    this.right.Rectangle = new Rectangle(this.Rectangle.X, this.Rectangle.Y + rectangle.Height, this.Rectangle.Width, this.Rectangle.Height - rectangle.Height);
                }

                // insert into the left child node as a leaf
                return(this.left.Insert(rectangle));
            }
Example #2
0
            public Node Insert(Rectangle rectangle)
            {
                // if this is not a leaf
                if (this.left != null)
                {
                  Node nodeLeft = this.left.Insert(rectangle);
                  if (nodeLeft != null)
                  {
                return nodeLeft;
                  }

                  return this.right.Insert(rectangle);
                }

                // already a rectangle in this node
                if (this.filled)
                {
                  return null;
                }

                // rectangle does not fit in bounds
                if (!rectangle.FitsIn(this.Rectangle))
                {
                  return null;
                }

                // perfectly snug fit
                if (rectangle.Compare(this.Rectangle) == 0)
                {
                  this.filled = true;
                  return this;
                }

                // split this node into two children
                this.left = new Node();
                this.right = new Node();

                int diffWidth = this.Rectangle.Width - rectangle.Width;
                int diffHeight = this.Rectangle.Height - rectangle.Height;

                // either split horizontally or vertically
                if (diffWidth > diffHeight)
                {
                  this.left.Rectangle = new Rectangle(this.Rectangle.X, this.Rectangle.Y, rectangle.Width, this.Rectangle.Height);
                  this.right.Rectangle = new Rectangle(this.Rectangle.X + rectangle.Width, this.Rectangle.Y, this.Rectangle.Width - rectangle.Width, this.Rectangle.Height);
                }
                else
                {
                  this.left.Rectangle = new Rectangle(this.Rectangle.X, this.Rectangle.Y, this.Rectangle.Width, rectangle.Height);
                  this.right.Rectangle = new Rectangle(this.Rectangle.X, this.Rectangle.Y + rectangle.Height, this.Rectangle.Width, this.Rectangle.Height - rectangle.Height);
                }

                // insert into the left child node as a leaf
                return this.left.Insert(rectangle);
            }