@ 2021-01-21T19:22:45Z <?php
/**
* Finds pixels with the lowest geometrical distance from given point (x, y) that match the criteria.
*/
function findGeometricallyClosestPixels(array $grid, int $x, int $y, string $criteria): array
{
// if grid is empty or we're out of bounds
if (empty($grid) || !isset($grid[$y]) || !isset($grid[0][$x])) {
return [];
}
if ($grid[$y][$x] === $criteria) {
return [[$x, $y]];
}
$result = [];
for ($offset = 1; $offset < calculateMaximumOffset($grid, $x, $y); $offset++) {
$result = array_merge($result, getClosestPixels($grid, $x, $y, $offset, $criteria));
if (!empty($result)) {
return $result;
}
for ($d = 1; $d <= $offset; $d++) {
$result = array_merge($result, getPixelsForNegativeYOffsets($grid, $x, $y, $offset, $d, $criteria));
$result = array_merge($result, getPixelsForPositiveYOffsets($grid, $x, $y, $offset, $d, $criteria));
if (!empty($result)) {
return $result;
}
}
}
return $result;
}
function getClosestPixels(array $grid, int $x, int $y, int $offset, string $criteria): array
{
$result = [];
if (isset($grid[$y][$x - $offset]) && $grid[$y][$x - $offset] === $criteria) {
$result[] = [$x - $offset, $y];
}
if (isset($grid[$y][$x + $offset]) && $grid[$y][$x + $offset] === $criteria) {
$result[] = [$x + $offset, $y];
}
if (isset($grid[$y - $offset][$x]) && $grid[$y - $offset][$x] === $criteria) {
$result[] = [$x, $y - $offset];
}
if (isset($grid[$y + $offset][$x]) && $grid[$y + $offset][$x] === $criteria) {
$result[] = [$x, $y + $offset];
}
return $result;
}
function getPixelsForNegativeYOffsets(array $grid, int $x, int $y, int $offset, int $d, string $criteria): array
{
if (!isset($grid[$y - $d])) {
return [];
}
$result = [];
if (isset($grid[$y - $d][$x - $offset]) && $grid[$y - $d][$x - $offset] === $criteria) {
$result[] = [$x - $offset, $y - $d];
}
if (isset($grid[$y - $d][$x + $offset]) && $grid[$y - $d][$x + $offset] === $criteria) {
$result[] = [$x + $offset, $y - $d];
}
if (!isset($grid[$y - $offset])) {
return $result;
}
if (isset($grid[$y - $offset][$x - $d]) && $grid[$y - $offset][$x - $d] === $criteria) {
$result[] = [$x - $d, $y - $offset];
}
if (isset($grid[$y - $offset][$x + $d]) && $grid[$y - $offset][$x + $d] === $criteria) {
$result[] = [$x + $d, $y - $offset];
}
return $result;
}
function getPixelsForPositiveYOffsets(array $grid, int $x, int $y, int $offset, int $d, string $criteria): array
{
if (!isset($grid[$y + $d])) {
return [];
}
$result = [];
if (isset($grid[$y + $d][$x + $offset]) && $grid[$y + $d][$x + $offset] === $criteria) {
$result[] = [$x + $offset, $y + $d];
}
if (isset($grid[$y + $d][$x - $offset]) && $grid[$y + $d][$x - $offset] === $criteria) {
$result[] = [$x - $offset, $y + $d];
}
if (!isset($grid[$y + $offset])) {
return $result;
}
if (isset($grid[$y + $offset][$x - $d]) && $grid[$y + $offset][$x - $d] === $criteria) {
$result[] = [$x - $d, $y + $offset];
}
if (isset($grid[$y + $offset][$x + $d]) && $grid[$y + $offset][$x + $d] === $criteria) {
$result[] = [$x + $d, $y + $offset];
}
return $result;
}
/**
* Calculates maximum offset needed to reach the edge of given grid for given starting point.
*
* Lowest possible offset is right in the middle of the greater grid dimension (width or height). Since grid starts
* at zero, if the point is positioned past the middle, then offset will equal its position. Otherwise, it will equal
* greater dimension minus its position increased by one (to compensate for the fact that dimensions are measured
* starting at 1, but positions start at 0).
*/
function calculateMaximumOffset(array $grid, int $x, int $y): int
{
$gridHeight = count($grid);
$gridWidth = count($grid[0]);
if ($gridHeight >= $gridWidth) {
$relevantDimensionValue = $gridHeight;
$relevantCoordinateValue = $y;
} else {
$relevantDimensionValue = $gridWidth;
$relevantCoordinateValue = $x;
}
$half = floor($relevantDimensionValue / 2);
if ($relevantCoordinateValue >= $half) {
return $relevantCoordinateValue;
}
return $relevantDimensionValue - ($relevantCoordinateValue + 1);
}
$heart = [
['w', 'w', 'w', 'r', 'r', 'w', 'w', 'w', 'w', 'w', 'r', 'r', 'w', 'w', 'w', 'w'],
['w', 'w', 'r', 'r', 'r', 'r', 'w', 'w', 'w', 'r', 'r', 'r', 'r', 'w', 'w', 'w'],
['w', 'r', 'r', 'r', 'r', 'r', 'r', 'w', 'r', 'r', 'r', 'r', 'r', 'r', 'w', 'w'],
['r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'w'],
['r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'w'],
['r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'w'],
['r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'w'],
['r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'w'],
['w', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'w', 'w'],
['w', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'w', 'w'],
['w', 'w', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'w', 'w', 'w'],
['w', 'w', 'w', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'w', 'w', 'w', 'w'],
['w', 'w', 'w', 'w', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'w', 'w', 'w', 'w', 'w'],
['w', 'w', 'w', 'w', 'w', 'r', 'r', 'r', 'r', 'r', 'w', 'w', 'w', 'w', 'w', 'w'],
['w', 'w', 'w', 'w', 'w', 'w', 'r', 'r', 'r', 'w', 'w', 'w', 'w', 'w', 'w', 'w'],
['w', 'w', 'w', 'w', 'w', 'w', 'w', 'r', 'w', 'w', 'w', 'w', 'w', 'w', 'w', 'w'],
];
var_dump(findGeometricallyClosestPixels($heart, 3, 0, 'w'));
Enable javascript to submit You have javascript disabled. You will not be able to edit any code.
Abusive script This script was stopped while abusing our resources
Here you find the average performance (time & memory) of each version. A grayed out version indicates it didn't complete successfully (based on exit-code).
Version System time (s) User time (s) Memory (MiB) 8.3.11 0.003 0.006 20.94 8.3.10 0.003 0.006 19.86 8.3.9 0.012 0.003 26.77 8.3.8 0.006 0.003 19.36 8.3.7 0.013 0.003 16.58 8.3.6 0.013 0.009 16.75 8.3.5 0.005 0.013 24.49 8.3.4 0.012 0.009 18.76 8.3.3 0.007 0.016 18.84 8.3.2 0.004 0.004 24.18 8.3.1 0.008 0.000 24.66 8.3.0 0.009 0.000 26.16 8.2.23 0.005 0.005 22.58 8.2.22 0.003 0.006 24.06 8.2.21 0.004 0.004 26.77 8.2.20 0.005 0.005 18.88 8.2.19 0.004 0.012 18.21 8.2.18 0.011 0.007 25.92 8.2.17 0.010 0.006 18.96 8.2.16 0.012 0.006 22.96 8.2.15 0.007 0.000 25.66 8.2.14 0.006 0.003 24.66 8.2.13 0.000 0.008 26.16 8.2.12 0.000 0.008 26.35 8.2.11 0.004 0.007 20.57 8.2.10 0.011 0.000 17.91 8.2.9 0.000 0.008 17.75 8.2.8 0.000 0.009 18.82 8.2.7 0.004 0.004 17.75 8.2.6 0.008 0.000 18.05 8.2.5 0.004 0.004 18.05 8.2.4 0.004 0.004 18.34 8.2.3 0.004 0.004 18.18 8.2.2 0.004 0.004 18.11 8.2.1 0.004 0.004 19.41 8.2.0 0.000 0.008 19.38 8.1.29 0.006 0.003 30.84 8.1.28 0.007 0.011 25.92 8.1.27 0.006 0.003 23.99 8.1.26 0.004 0.004 28.09 8.1.25 0.004 0.004 28.09 8.1.24 0.007 0.003 23.82 8.1.23 0.008 0.004 20.98 8.1.22 0.000 0.009 18.64 8.1.21 0.008 0.000 18.77 8.1.20 0.005 0.005 17.35 8.1.19 0.009 0.000 17.22 8.1.18 0.003 0.005 18.10 8.1.17 0.003 0.005 20.54 8.1.16 0.006 0.003 18.96 8.1.15 0.000 0.009 18.88 8.1.14 0.000 0.008 19.02 8.1.13 0.005 0.003 20.11 8.1.12 0.006 0.003 17.57 8.1.11 0.007 0.005 17.38 8.1.10 0.008 0.004 17.44 8.1.9 0.006 0.008 17.46 8.1.8 0.006 0.007 17.43 8.1.7 0.006 0.006 17.45 8.1.6 0.011 0.002 17.58 8.1.5 0.009 0.005 17.46 8.1.4 0.011 0.003 17.49 8.1.3 0.008 0.005 17.66 8.1.2 0.008 0.006 17.64 8.1.1 0.010 0.004 17.56 8.1.0 0.008 0.008 17.43 8.0.30 0.009 0.003 18.77 8.0.29 0.004 0.004 16.88 8.0.28 0.008 0.000 18.52 8.0.27 0.005 0.003 18.09 8.0.26 0.000 0.006 18.52 8.0.25 0.000 0.011 17.02 8.0.24 0.006 0.006 16.89 8.0.23 0.012 0.000 16.93 8.0.22 0.008 0.003 16.89 8.0.21 0.004 0.007 16.94 8.0.20 0.006 0.005 16.96 8.0.19 0.009 0.003 16.92 8.0.18 0.010 0.001 16.81 8.0.17 0.010 0.002 16.83 8.0.16 0.009 0.003 16.88 8.0.15 0.010 0.002 16.80 8.0.14 0.009 0.003 16.80 8.0.13 0.006 0.004 15.08 8.0.12 0.008 0.004 16.90 8.0.11 0.007 0.004 16.89 8.0.10 0.004 0.008 16.89 8.0.9 0.006 0.007 16.93 8.0.8 0.007 0.010 16.94 8.0.7 0.008 0.004 16.82 8.0.6 0.007 0.005 16.77 8.0.5 0.008 0.004 16.86 8.0.3 0.014 0.004 17.02 8.0.2 0.010 0.007 17.04 8.0.1 0.011 0.007 17.02 8.0.0 0.005 0.008 17.02 7.4.33 0.003 0.003 15.55 7.4.32 0.007 0.005 16.59 7.4.30 0.010 0.003 16.54 7.4.29 0.009 0.003 16.57 7.4.28 0.007 0.006 16.55 7.4.27 0.010 0.002 16.63 7.4.26 0.006 0.005 15.00 7.4.25 0.006 0.006 16.54 7.4.24 0.008 0.005 16.43 7.4.23 0.008 0.004 16.63 7.4.22 0.006 0.003 16.65 7.4.21 0.006 0.010 16.60 7.4.20 0.007 0.005 16.61 7.4.19 0.011 0.004 16.54 7.4.18 0.012 0.004 16.57 7.4.16 0.010 0.005 16.45 7.4.15 0.000 0.015 16.35 7.4.14 0.005 0.011 16.38 7.4.13 0.010 0.005 16.50 7.4.12 0.003 0.012 16.32 7.4.11 0.013 0.002 16.31 7.4.10 0.008 0.006 16.30 7.4.9 0.009 0.005 16.21 7.4.8 0.009 0.005 16.24 7.4.7 0.011 0.005 16.29 7.4.6 0.008 0.006 16.30 7.4.5 0.008 0.010 16.19 7.4.4 0.008 0.009 16.39 7.4.3 0.009 0.005 16.36 7.4.2 0.007 0.007 16.24 7.4.1 0.007 0.007 16.25 7.4.0 0.007 0.007 16.47 7.3.33 0.006 0.006 16.33 7.3.32 0.005 0.006 14.79 7.3.31 0.005 0.005 16.31 7.3.30 0.007 0.003 16.34 7.3.29 0.009 0.006 16.36 7.3.28 0.015 0.000 16.36 7.3.27 0.014 0.000 16.39 7.3.26 0.010 0.006 16.42 7.3.25 0.012 0.004 16.51 7.3.24 0.008 0.009 16.50 7.3.23 0.010 0.007 16.54 7.3.22 0.011 0.005 16.35 7.3.21 0.010 0.007 16.44 7.3.20 0.010 0.006 16.38 7.3.19 0.013 0.003 16.36 7.3.18 0.015 0.005 16.54 7.3.17 0.014 0.005 16.27 7.3.16 0.007 0.008 16.45 7.3.15 0.010 0.006 16.39 7.3.14 0.008 0.008 16.29 7.3.13 0.013 0.003 16.31 7.3.12 0.009 0.007 16.40 7.3.11 0.009 0.007 16.32 7.3.10 0.006 0.010 16.26 7.3.9 0.011 0.005 16.42 7.3.8 0.012 0.004 16.25 7.3.7 0.010 0.007 16.51 7.3.6 0.010 0.006 16.44 7.3.5 0.010 0.006 16.42 7.3.4 0.010 0.006 16.44 7.3.3 0.010 0.005 16.45 7.3.2 0.012 0.004 16.55 7.3.1 0.010 0.007 16.37 7.3.0 0.009 0.008 16.49
preferences:dark mode live preview ace vim emacs key bindings
28.18 ms | 403 KiB | 5 Q