Improve performance

This commit is contained in:
Dennis Eichhorn 2023-05-25 12:19:24 +00:00
parent feb054b8fb
commit fc2fd1343f
2 changed files with 20 additions and 25 deletions

View File

@ -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],

View File

@ -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
*