change heuristic to Best Area Fit

pull/1/head
cosmonaut 2022-07-22 12:20:14 -07:00
parent 812d142aa0
commit 943253add1
1 changed files with 6 additions and 6 deletions

View File

@ -226,7 +226,7 @@ RectPackContext* Cram_Internal_InitRectPacker(uint32_t width, uint32_t height)
return context; return context;
} }
/* Uses the short side fit heuristic. */ /* Uses the best area fit heuristic. */
/* TODO: make the heuristic configurable? */ /* TODO: make the heuristic configurable? */
void Cram_Internal_Score( void Cram_Internal_Score(
RectPackContext *context, RectPackContext *context,
@ -235,8 +235,8 @@ void Cram_Internal_Score(
PackScoreInfo *scoreInfo PackScoreInfo *scoreInfo
) { ) {
Rect *freeRect; Rect *freeRect;
int32_t areaFit;
int32_t shortestSide; int32_t shortestSide;
int32_t longestSide;
int32_t i; int32_t i;
scoreInfo->score = INT32_MAX; scoreInfo->score = INT32_MAX;
@ -248,13 +248,13 @@ void Cram_Internal_Score(
if (freeRect->w >= width && freeRect->h >= height) if (freeRect->w >= width && freeRect->h >= height)
{ {
areaFit = freeRect->w * freeRect->h - width * height;
shortestSide = Cram_min(freeRect->w - width, freeRect->h - height); shortestSide = Cram_min(freeRect->w - width, freeRect->h - height);
longestSide = Cram_max(freeRect->w - width, freeRect->h - height);
if (shortestSide < scoreInfo->score || (shortestSide == scoreInfo->score && longestSide < scoreInfo->secondaryScore)) if (areaFit < scoreInfo->score || (areaFit == scoreInfo->score && shortestSide < scoreInfo->secondaryScore))
{ {
scoreInfo->score = shortestSide; scoreInfo->score = areaFit;
scoreInfo->secondaryScore = longestSide; scoreInfo->secondaryScore = shortestSide;
scoreInfo->x = freeRect->x; scoreInfo->x = freeRect->x;
scoreInfo->y = freeRect->y; scoreInfo->y = freeRect->y;
} }