// --------------------------------------------------------------------------- bool Fits(BinRect rect1, BinRect rect2, bool allowRotation) { // Check to see if rect1 fits in rect2, and rotate rect1 if that will // enable it to fit. if (rect1.w <= rect2.w && rect1.h <= rect2.h) { return(true); } else if (allowRotation && rect1.h <= rect2.w && rect1.w <= rect2.h) { rect1.Rotate(); return(true); } else { return(false); } }
// --------------------------------------------------------------------------- void Split(int pack, int rect) { //assert(PackIsValid(pack)); //assert(RectIsValid(rect)); int i = pack; int j = rect; // Split the working area either horizontally or vertically with respect // to the rect we're storing, such that we get the largest possible child // area. BinRect left = m_packs[i].Copy(); BinRect right = m_packs[i].Copy(); BinRect bottom = m_packs[i].Copy(); BinRect top = m_packs[i].Copy(); left.y += m_rects[j].h; left.w = m_rects[j].w; left.h -= m_rects[j].h; right.x += m_rects[j].w; right.w -= m_rects[j].w; bottom.x += m_rects[j].w; bottom.h = m_rects[j].h; bottom.w -= m_rects[j].w; top.y += m_rects[j].h; top.h -= m_rects[j].h; int maxLeftRightArea = left.GetArea(); if (right.GetArea() > maxLeftRightArea) { maxLeftRightArea = right.GetArea(); } int maxBottomTopArea = bottom.GetArea(); if (top.GetArea() > maxBottomTopArea) { maxBottomTopArea = top.GetArea(); } if (maxLeftRightArea > maxBottomTopArea) { if (left.GetArea() > right.GetArea()) { m_packs.Add(left); m_packs.Add(right); } else { m_packs.Add(right); m_packs.Add(left); } } else { if (bottom.GetArea() > top.GetArea()) { m_packs.Add(bottom); m_packs.Add(top); } else { m_packs.Add(top); m_packs.Add(bottom); } } // This pack area now represents the rect we've just stored, so save the // relevant info to it, and assign children. m_packs[i].w = m_rects[j].w; m_packs[i].h = m_rects[j].h; m_packs[i].obj = m_rects[j].obj; m_packs[i].rotated = m_rects[j].rotated; m_packs[i].children[0] = m_packs.Count - 2; m_packs[i].children[1] = m_packs.Count - 1; // Done with the rect m_rects[j].packed = true; m_rects[j].x = m_packs[i].x; m_rects[j].y = m_packs[i].y; }