diff --git a/src/cram.c b/src/cram.c index ea588c3..07c7899 100644 --- a/src/cram.c +++ b/src/cram.c @@ -226,7 +226,7 @@ RectPackContext* Cram_Internal_InitRectPacker(uint32_t width, uint32_t height) return context; } -/* Uses the short side fit heuristic. */ +/* Uses the best area fit heuristic. */ /* TODO: make the heuristic configurable? */ void Cram_Internal_Score( RectPackContext *context, @@ -235,8 +235,8 @@ void Cram_Internal_Score( PackScoreInfo *scoreInfo ) { Rect *freeRect; + int32_t areaFit; int32_t shortestSide; - int32_t longestSide; int32_t i; scoreInfo->score = INT32_MAX; @@ -248,13 +248,13 @@ void Cram_Internal_Score( if (freeRect->w >= width && freeRect->h >= height) { + areaFit = freeRect->w * freeRect->h - width * 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->secondaryScore = longestSide; + scoreInfo->score = areaFit; + scoreInfo->secondaryScore = shortestSide; scoreInfo->x = freeRect->x; scoreInfo->y = freeRect->y; }