Beispiel #1
0
 public RectNode(Rectangle rect, RectNode childA, RectNode childB)
 {
     this.rect = rect;
     this.childA = childA;
     this.childB = childB;
 }
Beispiel #2
0
 public bool InsertAndUpdate(ref Rectangle rect)
 {
     Normalize(ref rect);
     if (IsLeaf()) // insert rect here
     {
         if (!IsTaken() && this.rect.GetNormalized().Contains(rect)) // check if it fits
         {
             int dw = this.rect.Width - rect.Width;
             int dh = this.rect.Height - rect.Height;
             if (dw > dh) // split horizontally
             {
                 childA = new RectNode(new Rectangle(this.rect.X, this.rect.Y,
                     rect.Width, this.rect.Height));
                 childB = new RectNode(new Rectangle(this.rect.X + rect.Width, this.rect.Y,
                     this.rect.Width - rect.Width, this.rect.Height));
             }
             else // split vertically
             {
                 childA = new RectNode(new Rectangle(this.rect.X, this.rect.Y,
                     this.rect.Width, rect.Height));
                 childB = new RectNode(new Rectangle(this.rect.X, this.rect.Y + rect.Height,
                     this.rect.Width, this.rect.Height - rect.Height));
             }
             childA.childA = new RectNode(new Rectangle(this.rect.X, this.rect.Y,
                 rect.Width, rect.Height));
             childA.childA.taken = true;
             rect.X = this.rect.X;
             rect.Y = this.rect.Y;
             if (childA.childA.rect.Width == childA.rect.Width)
             {
                 childA.childB = new RectNode(new Rectangle(
                     childA.rect.X,
                     childA.rect.Y + childA.childA.rect.Height,
                     childA.rect.Width,
                     childA.rect.Height - childA.childA.rect.Height));
             }
             else if (childA.childA.rect.Height == childA.rect.Height)
             {
                 childA.childB = new RectNode(new Rectangle(
                     childA.rect.X + childA.childA.rect.Width,
                     childA.rect.Y,
                     childA.rect.Width - childA.childA.rect.Width,
                     childA.rect.Height));
             }
             return true;
         }
         else
         {
             return false;
         }
     }
     else
     {
         return (childA != null && childA.InsertAndUpdate(ref rect)) ||
             (childB != null && childB.InsertAndUpdate(ref rect));
     }
 }