duh, best-fit should tiebreak using waste, not prioritize waste (which makes bizarre towers)

This commit is contained in:
Sean Barrett 2014-11-25 15:06:27 -08:00
parent 2c56e11c59
commit e9151589ca

View File

@ -1,4 +1,4 @@
// stb_rect_pack.h - v0.01 - public domain - rectangle packing // stb_rect_pack.h - v0.02 - public domain - rectangle packing
// Sean Barrett 2014 // Sean Barrett 2014
// //
// Useful for e.g. packing rectangular textures into an atlas. // Useful for e.g. packing rectangular textures into an atlas.
@ -311,9 +311,9 @@ static stbrp__findresult stbrp__skyline_find_best_pos(stbrp_context *c, int widt
} }
} else { } else {
// best-fit // best-fit
if (waste < best_waste) {
// can only use it if it first vertically
if (y + height <= c->height) { if (y + height <= c->height) {
// can only use it if it first vertically
if (y < best_y || (y == best_y && waste < best_waste)) {
best_y = y; best_y = y;
best_waste = waste; best_waste = waste;
best = prev; best = prev;
@ -361,14 +361,17 @@ static stbrp__findresult stbrp__skyline_find_best_pos(stbrp_context *c, int widt
} }
assert(node->next->x > xpos && node->x <= xpos); assert(node->next->x > xpos && node->x <= xpos);
y = stbrp__skyline_find_min_y(c, node, xpos, width, &waste); y = stbrp__skyline_find_min_y(c, node, xpos, width, &waste);
if (waste <= best_waste && y + height < c->height) { if (y + height < c->height) {
if (waste < best_waste || y < best_y || (y==best_y && xpos < best_x)) { if (y <= best_y) {
if (y < best_y || waste < best_waste || (waste==best_waste && xpos < best_x)) {
best_x = xpos; best_x = xpos;
assert(y <= best_y);
best_y = y; best_y = y;
best_waste = waste; best_waste = waste;
best = prev; best = prev;
} }
} }
}
tail = tail->next; tail = tail->next;
} }
} }
@ -472,6 +475,17 @@ static int rect_height_compare(const void *a, const void *b)
return (p->w > q->w) ? -1 : (p->w < q->w); return (p->w > q->w) ? -1 : (p->w < q->w);
} }
static int rect_width_compare(const void *a, const void *b)
{
stbrp_rect *p = (stbrp_rect *) a;
stbrp_rect *q = (stbrp_rect *) b;
if (p->w > q->w)
return -1;
if (p->w < q->w)
return 1;
return (p->h > q->h) ? -1 : (p->h < q->h);
}
static int rect_original_order(const void *a, const void *b) static int rect_original_order(const void *a, const void *b)
{ {
stbrp_rect *p = (stbrp_rect *) a; stbrp_rect *p = (stbrp_rect *) a;