mirror of
https://github.com/Karaka-Management/phpOMS.git
synced 2026-02-07 04:58:40 +00:00
impl. early return
This commit is contained in:
parent
50ca46b5a9
commit
ebd709d4cb
|
|
@ -45,36 +45,37 @@ final class MonotoneChain
|
||||||
*/
|
*/
|
||||||
public static function createConvexHull(array $points) : array
|
public static function createConvexHull(array $points) : array
|
||||||
{
|
{
|
||||||
if (($n = \count($points)) > 1) {
|
if (($n = \count($points)) < 2) {
|
||||||
\uasort($points, [self::class, 'sort']);
|
return $points;
|
||||||
|
|
||||||
$k = 0;
|
|
||||||
$result = [];
|
|
||||||
|
|
||||||
// Lower hull
|
|
||||||
for ($i = 0; $i < $n; ++$i) {
|
|
||||||
while ($k >= 2 && self::cross($result[$k - 2], $result[$k - 1], $points[$i]) <= 0) {
|
|
||||||
--$k;
|
|
||||||
}
|
|
||||||
|
|
||||||
$result[$k++] = $points[$i];
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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) {
|
|
||||||
--$k;
|
|
||||||
}
|
|
||||||
|
|
||||||
$result[$k++] = $points[$i];
|
|
||||||
}
|
|
||||||
|
|
||||||
\ksort($result);
|
|
||||||
|
|
||||||
return \array_slice($result, 0, $k - 1);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return $points;
|
\uasort($points, [self::class, 'sort']);
|
||||||
|
|
||||||
|
$k = 0;
|
||||||
|
$result = [];
|
||||||
|
|
||||||
|
// Lower hull
|
||||||
|
for ($i = 0; $i < $n; ++$i) {
|
||||||
|
while ($k >= 2 && self::cross($result[$k - 2], $result[$k - 1], $points[$i]) <= 0) {
|
||||||
|
--$k;
|
||||||
|
}
|
||||||
|
|
||||||
|
$result[$k++] = $points[$i];
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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) {
|
||||||
|
--$k;
|
||||||
|
}
|
||||||
|
|
||||||
|
$result[$k++] = $points[$i];
|
||||||
|
}
|
||||||
|
|
||||||
|
\ksort($result);
|
||||||
|
|
||||||
|
/** @return array<int, array{x:int|float, y:int|float}> */
|
||||||
|
return \array_slice($result, 0, $k - 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user