Exemple #1
0
            public PackerNode Insert(PackerRect rect)
            {
                PackerNode result;

                if (child[0] != null && child[1] != null)
                {
                    result = child[0].Insert(rect);
                    if (result != null)
                    {
                        return(result);
                    }

                    result = child[1].Insert(rect);
                    return(result);
                }


                //if there's already a lightmap here, return
                if (this.rect != null)
                {
                    return(null);
                }

                // if we're too small, return
                if (rect.width > this.width || rect.height > this.height)
                {
                    return(null);
                }

                // if we're just right, accept
                if (rect.width == this.width && rect.height == this.height)
                {
                    rect.done = true;
                    this.rect = rect;
                    return(this);
                }

                // otherwise, gotta split this node and create some kids

                //decide which way to split
                int dw = this.width - rect.width;
                int dh = this.height - rect.height;

                if (dw > dh)
                {
                    this.child[0] = new PackerNode(this.x, this.y, rect.width, this.height);
                    this.child[1] = new PackerNode(this.x + rect.width, this.y, this.width - (rect.width), this.height);
                }
                else
                {
                    this.child[0] = new PackerNode(this.x, this.y, this.width, rect.height);
                    this.child[1] = new PackerNode(this.x, this.y + rect.height, this.width, this.height - (rect.height));
                }

                //insert into secondChannel child we created
                return(child[0].Insert(rect));
            }
Exemple #2
0
 public PackerNode(int x, int y, int width, int height)
 {
     this.x        = x;
     this.y        = y;
     this.width    = width;
     this.height   = height;
     this.rect     = null;
     this.child    = new PackerNode[2];
     this.child[0] = null;
     this.child[1] = null;
 }
Exemple #3
0
        public void AddRect(int width, int height, T key)
        {
            if (width <= 0 || height <= 0)
            {
                throw new Exception();
            }

            PackerRect rect = new PackerRect();

            _rectList.Add(rect);

            rect.width  = width;
            rect.height = height;
            rect.key    = key;
            rect.node   = null;
            rect.done   = false;
        }