edit: Diagonal movement is allowed
I have a 5x5 Grid (just temp. to make it easier on myself though will grow to 30x30 and larger) and I'm trying to determine a grid count between 2 grids, however, it's doing my head in and I was hoping maybe someone can point me in the right direction. What I'm trying to achieve is basically a user chooses 2 grids i.e. 3004
and 3017
it should then find the shortest route and count the grids. For instance, the grid count between 3004
and 3017
should be 4
, the grid count between 3001
and 3005
should be 5
, the grid count between 3001
and 3017
should be 4
, the grid count between 3001
and 3002
should be 2
, etc.
The Grid:
------------------------------------
| 3001 | 3002 | 3003 | 3004 | 3005 |
------------------------------------
| 3006 | 3007 | 3008 | 3009 | 3010 |
------------------------------------
| 3011 | 3012 | 3013 | 3014 | 3015 |
------------------------------------
| 3016 | 3017 | 3018 | 3019 | 3020 |
------------------------------------
| 3021 | 3022 | 3023 | 3024 | 3025 |
------------------------------------
My array is structured in the following way:
$grids = array(
1 => array(
1 => 3001,
2 => 3002,
3 => 3003,
4 => 3004,
5 => 3005
),
2 => array(
1 => 3006,
2 => 3007,
3 => 3008,
4 => 3009,
5 => 3010
),
3 => array(
1 => 3011,
2 => 3012,
3 => 3013,
4 => 3014,
5 => 3015
),
4 => array(
1 => 3016,
2 => 3017,
3 => 3018,
4 => 3019,
5 => 3020
),
5 => array(
1 => 3021,
2 => 3022,
3 => 3023,
4 => 3024,
5 => 3025
),
);
What I have tried:
I realized I could use the good old Hypotenuse a2 + b2 = c2
That worked pretty good for a few examples, but, not everytime. For example, if I plugged in 3004
and 3017
the grid count should be 4
, but, it spits out 5
or if I plugged in 3001
and 3025
the grid count would be 7
, which is outside of the grid itself. I do understand why those results are coming up so I wanted to check if there is a way to improve the formula to make it work somehow?
This issue is killing me as I'm so close to to a solution I can taste it.
A big thank you for all the help in advance!
My current code: (please excuse the messiness)
<style>
.grid {
display: inline-block;
line-height: 50px; padding-left: 10px; padding-right: 10px;
border:1px solid black;
}
.grid.active {
background: red;
}
</style>
<?php
/*
3001 3002 3003 3004 3005
3006 3007 3008 3009 3010
3011 3012 3013 3014 3015
3016 3017 3018 3019 3020
3021 3022 3023 3024 3025
*/
$from = 3001;
$to = 3025;
$grids = array(
1 => array(
1 => 3001,
2 => 3002,
3 => 3003,
4 => 3004,
5 => 3005
),
2 => array(
1 => 3006,
2 => 3007,
3 => 3008,
4 => 3009,
5 => 3010
),
3 => array(
1 => 3011,
2 => 3012,
3 => 3013,
4 => 3014,
5 => 3015
),
4 => array(
1 => 3016,
2 => 3017,
3 => 3018,
4 => 3019,
5 => 3020
),
5 => array(
1 => 3021,
2 => 3022,
3 => 3023,
4 => 3024,
5 => 3025
),
);
$from_cells = 1;
$from_rows = 1;
foreach ( $grids as $y => $xs )
{
$from_cells = 1;
foreach ( $xs as $x => $postcode )
{
if ( $postcode == $from )
{
break 2;
}
else
{
$from_cells++;
}
}
$from_rows++;
}
$to_cells = 1;
$to_rows = 1;
foreach ( $grids as $y => $xs )
{
$to_cells = 1;
foreach ( $xs as $x => $postcode )
{
if ( $postcode == $to )
{
break 2;
}
else
{
$to_cells++;
}
}
$to_rows++;
}
echo "From<hr/>X: $from_cells | Y: $from_rows<br/><hr/>To<hr/>X: $to_cells | Y: $to_rows";
$leg1 = 0;
$leg2 = 0;
$cell_short = ($from_cells < $to_cells) ? $from_cells : $to_cells;
$cell_long = ($from_cells > $to_cells) ? $from_cells : $to_cells;
for ( $i=$cell_short; $i <= $cell_long; $i++ )
{
$leg1 = $i;
}
$row_short = ($from_rows < $to_rows) ? $from_rows : $to_rows;
$row_long = ($from_rows > $to_rows) ? $from_rows : $to_rows;
for ( $i=$row_short; $i <= $row_long; $i++ )
{
$leg2 = $i;
}
echo "<hr/>Length: $leg1<br/>Height: $leg2";
$grid_count = pow($leg1,2) + pow($leg2,2);
$grid_count = floor(sqrt($grid_count));
echo "<hr/>Grids: $grid_count<hr/>";
foreach ( $grids as $y => $xs )
{
foreach ( $xs as $x => $postcode )
{
if ( $postcode == $from or $postcode == $to )
{
echo '<div class="grid active">'.$postcode.'</div>';
}
else
{
echo '<div class="grid">'.$postcode.'</div>';
}
}
echo '<br/>';
}
If any other information is needed please let me know and I'll throw it in.