Skip to content

Commit

Permalink
Fix logic bug meaning some flipped rects were placed overlapping the …
Browse files Browse the repository at this point in the history
…edge of the bin.
  • Loading branch information
neekfenwick committed Nov 1, 2022
1 parent f0291f9 commit 43887e0
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 10 deletions.
6 changes: 3 additions & 3 deletions src/Algorithms/BestAreaFit.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ public static function findNewPosition(
}
}

if ($rectangle->getAllowFlip() == FlipType::ForceFlip ||
($bin->isFlipAllowed() && $rectangle->getWidth() > $rectangle->getHeight())) {

$tryFlip = $rectangle->getAllowFlip() == FlipType::ForceFlip ||
($bin->isFlipAllowed() && $rectangle->getWidth() > $rectangle->getHeight());
if ($tryFlip && ($freeRect->getWidth() >= $rectangle->getHeight() && $freeRect->getHeight() >= $rectangle->getWidth())) {
$leftoverHoriz = abs($freeRect->getWidth() - $rectangle->getHeight());
$leftoverVert = abs($freeRect->getHeight() - $rectangle->getWidth());
$shortSideFit = min($leftoverHoriz, $leftoverVert);
Expand Down
5 changes: 3 additions & 2 deletions src/Algorithms/BestLongSideFit.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@ public static function findNewPosition(
}
}

if ($rectangle->getAllowFlip() == FlipType::ForceFlip ||
($bin->isFlipAllowed() && $rectangle->getWidth() > $rectangle->getHeight())) {
$tryFlip = $rectangle->getAllowFlip() == FlipType::ForceFlip ||
($bin->isFlipAllowed() && $rectangle->getWidth() > $rectangle->getHeight());
if ($tryFlip && ($freeRect->getWidth() >= $rectangle->getHeight() && $freeRect->getHeight() >= $rectangle->getWidth())) {
$leftoverHoriz = abs($freeRect->getWidth() - $rectangle->getHeight());
$leftoverVert = abs($freeRect->getHeight() - $rectangle->getWidth());
$shortSideFit = min($leftoverHoriz, $leftoverVert);
Expand Down
7 changes: 4 additions & 3 deletions src/Algorithms/BestShortSideFit.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@ public static function findNewPosition(
}
}

if ($rectangle->getAllowFlip() == FlipType::ForceFlip ||
($bin->isFlipAllowed() && $rectangle->getWidth() > $rectangle->getHeight())) {
$tryFlip = $rectangle->getAllowFlip() == FlipType::ForceFlip ||
($bin->isFlipAllowed() && $rectangle->getWidth() > $rectangle->getHeight());
if ($tryFlip && ($freeRect->getWidth() >= $rectangle->getHeight() && $freeRect->getHeight() >= $rectangle->getWidth())) {
$leftoverHoriz = abs($freeRect->getWidth() - $rectangle->getHeight());
$leftoverVert = abs($freeRect->getHeight() - $rectangle->getWidth());
$shortSideFit = min($leftoverHoriz, $leftoverVert);
Expand All @@ -47,7 +48,7 @@ public static function findNewPosition(
$bestNode->setX($freeRect->getX());
$bestNode->setY($freeRect->getY());
$bestNode->rotate();

$bestShortSideFit = $shortSideFit;
$bestLongSideFit = $longSideFit;
}
Expand Down
5 changes: 3 additions & 2 deletions src/Algorithms/BottomLeft.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@ public static function findNewPosition(
}
}

if ($rectangle->getAllowFlip() == FlipType::ForceFlip ||
($bin->isFlipAllowed() && $rectangle->getWidth() > $rectangle->getHeight())) {
$tryFlip = $rectangle->getAllowFlip() == FlipType::ForceFlip ||
($bin->isFlipAllowed() && $rectangle->getWidth() > $rectangle->getHeight());
if ($tryFlip && ($freeRect->getWidth() >= $rectangle->getHeight() && $freeRect->getHeight() >= $rectangle->getWidth())) {
$topSideY = $freeRect->getY() + $rectangle->getWidth();
if ($topSideY < $bestY || ($topSideY == $bestY && $freeRect->getX() < $bestX)) {
$bestNode = RectangleFactory::fromRectangle($rectangle);
Expand Down

0 comments on commit 43887e0

Please sign in to comment.