J'ai besoin d'un algorithme qui divise un grand rectangle de taille statique en petits rectangles. Une implémentation parfaite pour moi ressembler à ceci:Partitionnement d'un grand rectangle à un petit rectangle (emballage 2D)
struct RECT
{
int l,t,r,b;
};
class BigRect
{
public:
// width and height of big rect
BigRect(unsigned width, unsigned height);
// returns -1 if rect cannot be allocated, otherwise returns id of found rect
int GetRect(unsigned width, unsigned height, RECT &out);
// returns allocated rect to big rectangle
void FreeRect(int id);
};
void test()
{
BigRect r(10, 10);
RECT out;
r.GetRect(4, 4, out); // rect found ({0,0,4,4} for example), returns 1
r.GetRect(5, 5, out); // rect found ({4,0,9,5} for example), returns 2
r.GetRect(6, 6, out); // no place found for rect, returns -1
r.FreeRect(2); // add {4,0,9,5} back to rect
r.GetRect(6, 6, out); // rect found (4,0,10,6)
}
donc j'ai besoin algorithme pour GetRect
et FreeRect
méthodes. Toutes les idées et les liens seraient appréciés.
Cela sent comme les devoirs. –
Existe-t-il des restrictions sur l'attribution des sous-rectangles? Par exemple. Y at-il un objectif d'emballer les rectangles efficacement, ou pouvez-vous simplement les coller où ils vont? – verdesmarald
@ Jean-Paul Calderone. Ce n'est pas les devoirs. @veredesmarald bien sûr il vaudrait mieux les allouer efficacement. –