From fc2fd1343f0eadbf5e39b0a7a8e8a1c7dc9afeab Mon Sep 17 00:00:00 2001 From: Dennis Eichhorn Date: Thu, 25 May 2023 12:19:24 +0000 Subject: [PATCH] Improve performance --- Image/Skew.php | 17 +++++++------ Math/Geometry/ConvexHull/MonotoneChain.php | 28 ++++++++-------------- 2 files changed, 20 insertions(+), 25 deletions(-) diff --git a/Image/Skew.php b/Image/Skew.php index 4ae1f3051..092477a84 100755 --- a/Image/Skew.php +++ b/Image/Skew.php @@ -132,12 +132,15 @@ final class Skew $rotated = [[]]; + $cXArr = []; + for ($j = 0; $j < $dim[1]; ++$j) { + $cXArr[] = $j - $dim[1] / 2.0; // center + } + for ($i = 0; $i < $dim[0]; ++$i) { $cY = $i - $dim[0] / 2.0; // center - for ($j = 0; $j < $dim[1]; ++$j) { - $cX = $j - $dim[1] / 2.0; // center - + foreach ($cXArr as $j => $cX) { $x = $cos * $cX + $sin * $cY + $dim[1] / 2.0; $y = -$sin * $cX + $cos * $cY + $dim[0] / 2.0; @@ -162,11 +165,11 @@ final class Skew */ private static function getNearestValue(array $pixel, array $dim, float $x, float $y) : int { - $xLow = \min((int) $x, $dim[1] - 1); - $xHigh = \min((int) \ceil($x), $dim[1] - 1); + $xLow = ($x < 0) ? 0 : (($x > ($dim[1] - 1)) ? ($dim[1] - 1) : (int) $x); + $xHigh = ($xLow == ($dim[1] - 1)) ? $xLow : ($xLow + 1); - $yLow = \min((int) $y, $dim[0] - 1); - $yHigh = \min((int) \ceil($y), $dim[0] - 1); + $yLow = ($y < 0) ? 0 : (($y > ($dim[0] - 1)) ? ($dim[0] - 1) : (int) $y); + $yHigh = ($yLow == ($dim[0] - 1)) ? $yLow : ($yLow + 1); $points = [ [$xLow, $yLow], diff --git a/Math/Geometry/ConvexHull/MonotoneChain.php b/Math/Geometry/ConvexHull/MonotoneChain.php index ae0460657..e82d0902d 100755 --- a/Math/Geometry/ConvexHull/MonotoneChain.php +++ b/Math/Geometry/ConvexHull/MonotoneChain.php @@ -56,7 +56,11 @@ final class MonotoneChain // Lower hull for ($i = 0; $i < $n; ++$i) { - while ($k >= 2 && self::cross($result[$k - 2], $result[$k - 1], $points[$i]) <= 0) { + while ($k >= 2 + && ($result[$k - 1]['x'] - $result[$k - 2]['x']) * ($points[$i]['y'] - $result[$k - 2]['y']) + - ($result[$k - 1]['y'] - $result[$k - 2]['y']) * ($points[$i]['x'] - $result[$k - 2]['x'] + ) <= 0 + ) { --$k; } @@ -65,7 +69,11 @@ final class MonotoneChain // Upper hull for ($i = $n - 2, $t = $k + 1; $i >= 0; --$i) { - while ($k >= $t && self::cross($result[$k - 2], $result[$k - 1], $points[$i]) <= 0) { + while ($k >= $t + && ($result[$k - 1]['x'] - $result[$k - 2]['x']) * ($points[$i]['y'] - $result[$k - 2]['y']) + - ($result[$k - 1]['y'] - $result[$k - 2]['y']) * ($points[$i]['x'] - $result[$k - 2]['x'] + ) <= 0 + ) { --$k; } @@ -78,22 +86,6 @@ final class MonotoneChain return \array_slice($result, 0, $k - 1); } - /** - * Counter clock wise turn? - * - * @param array{x:int|float, y:int|float} $a Point a - * @param array{x:int|float, y:int|float} $b Point b - * @param array{x:int|float, y:int|float} $c Point c - * - * @return float - * - * @since 1.0.0 - */ - private static function cross(array $a, array $b, array $c) : float - { - return ($b['x'] - $a['x']) * ($c['y'] - $a['y']) - ($b['y'] - $a['y']) * ($c['x'] - $a['x']); - } - /** * Sort by x coordinate then by z coordinate *