diff --git a/src/Algorithms/BestAreaFit.php b/src/Algorithms/BestAreaFit.php index 5eb69ce..aab7ea5 100644 --- a/src/Algorithms/BestAreaFit.php +++ b/src/Algorithms/BestAreaFit.php @@ -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); diff --git a/src/Algorithms/BestLongSideFit.php b/src/Algorithms/BestLongSideFit.php index a066350..c23be53 100644 --- a/src/Algorithms/BestLongSideFit.php +++ b/src/Algorithms/BestLongSideFit.php @@ -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); diff --git a/src/Algorithms/BestShortSideFit.php b/src/Algorithms/BestShortSideFit.php index a8d4d70..0edafa7 100644 --- a/src/Algorithms/BestShortSideFit.php +++ b/src/Algorithms/BestShortSideFit.php @@ -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); @@ -47,7 +48,7 @@ public static function findNewPosition( $bestNode->setX($freeRect->getX()); $bestNode->setY($freeRect->getY()); $bestNode->rotate(); - + $bestShortSideFit = $shortSideFit; $bestLongSideFit = $longSideFit; } diff --git a/src/Algorithms/BottomLeft.php b/src/Algorithms/BottomLeft.php index 062b195..aa654dd 100644 --- a/src/Algorithms/BottomLeft.php +++ b/src/Algorithms/BottomLeft.php @@ -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);