Example #1
0
        /// <summary>
        ///  Tries packing all added rects, and returns number of rects that could not be packed.
        /// </summary>
        /// <param name="minX"></param>
        /// <param name="minY"></param>
        /// <param name="maxX"></param>
        /// <param name="maxY"></param>
        /// <returns></returns>
        public int Pack(int minX, int minY, int maxX, int maxY)
        {
            int count = 0;

            for (int i = 0; i < _rectList.Count; i++)
            {
                if (!_rectList[i].done)
                {
                    count++;
                }
            }

            int index = 0;

            _root = null;

            while (count > 0)
            {
                int max = 0;
                for (int i = 0; i < _rectList.Count; i++)
                {
                    if (_rectList[i].done)
                    {
                        continue;
                    }


                    int k = _rectList[i].width * _rectList[i].height;
                    if (k > max)
                    {
                        max   = k;
                        index = i;
                    }
                }


                if (_root == null)
                {
                    _root = new PackerNode(minX, minY, maxX, maxY);
                }


                _rectList[index].node = _root.Insert(_rectList[index]);
                if (_rectList[index].node == null)
                {
                    return(count);
                }

                count--;
            }

            return(count);
        }
Example #2
0
 public void ClearRects()
 {
     _rectList = new List <PackerRect>();
     _root     = null;
 }