我的结果数组在这里会发生什么?我希望结果数组的大小等于输入数组,但缺少3个条目..它们去了哪里?他们没有像他们原本那样去追回......
代码:
<?php
init();
$list_xy = array(
0 => array(
'id' => '308',
'x' => '37',
'y' => '63'
),
1 => array(
'id' => '963',
'x' => '38',
'y' => '134'
),
2 => array(
'id' => '385',
'x' => '39',
'y' => '132'
),
3 => array(
'id' => '1231',
'x' => '50',
'y' => '199'
),
4 => array(
'id' => '788',
'x' => '51',
'y' => '59'
),
5 => array(
'id' => '1151',
'x' => '53',
'y' => '61'
),
6 => array(
'id' => '671',
'x' => '55',
'y' => '60'
),
7 => array(
'id' => '1487',
'x' => '55',
'y' => '55'
)
);
$sorted_list_xy = sort_by_xy_distance($list_xy);
$sorted_list_xy_size = count($sorted_list_xy, COUNT_NORMAL);
$list_xy_size = count($list_xy, COUNT_NORMAL);
var_dump($sorted_list_xy_size == $list_xy_size ? "looks right" : "something is wrong", $sorted_list_xy_size, $list_xy_size);
die("died");
function sort_by_xy_distance($input_list)
{
$ret = array();
$a = $input_list[0];
array_push($ret, $input_list[0]);
$input_list[0] = null;
$i = 1;
for ($i = 1; $i < count($input_list); ++$i) {
if ($input_list[$i] == null) {
echo 'already added to list..';
continue;
}
$ii = 1;
$tmpdistance = 0;
$nearest = array(
'index' => -1,
'distance' => PHP_INT_MAX
);
for ($ii = 1; $ii < count($input_list); ++$ii) {
if ($input_list[$ii] == null || $ii == $i) {
//echo 'already added to list..';
continue;
}
$tmpdistance = abs($input_list[$ii]['x'] - $a['x']) + abs($input_list[$ii]['y'] - $a['y']);
if ($tmpdistance < $nearest['distance']) {
$nearest['index'] = $ii;
$nearest['distance'] = $tmpdistance;
}
}
assert($nearest['index'] != -1);
array_push($ret, $input_list[$nearest['index']]);
$a = $input_list[$nearest['index']];
$input_list[$nearest['index']] = null;
}
return $ret;
}
function init()
{
error_reporting(E_ALL);
set_error_handler("exception_error_handler");
}
function exception_error_handler($errno, $errstr, $errfile, $errline)
{
if (!(error_reporting() & $errno)) {
// This error code is not included in error_reporting
return;
}
throw new ErrorException($errstr, 0, $errno, $errfile, $errline);
}
输出:
已添加到list ..已添加到list..already已添加到 list..string(18)“出错了”int(5)int(8)去世了
预期输出:(类似于)
已添加到list ..已添加到list..already已添加到 list..string(11)“看起来正确”int(8)int(8)去世
我期望列表转到: 一个列表,其中与下一个[x] [y]的差异尽可能小,这将是:
array(8) {
[0]=>
array(3) {
["id"]=>
string(3) "308"
["x"]=>
string(2) "37"
["y"]=>
string(2) "63"
}
[1]=>
array(3) {
["id"]=>
string(3) "788"
["x"]=>
string(2) "51"
["y"]=>
string(2) "59"
}
[2]=>
array(3) {
["id"]=>
string(4) "1151"
["x"]=>
string(2) "53"
["y"]=>
string(2) "61"
}
[3]=>
array(3) {
["id"]=>
string(3) "671"
["x"]=>
string(2) "55"
["y"]=>
string(2) "60"
}
[4]=>
array(3) {
["id"]=>
string(4) "1487"
["x"]=>
string(2) "55"
["y"]=>
string(2) "55"
}
[5]=>
array(3) {
["id"]=>
string(3) "385"
["x"]=>
string(2) "39"
["y"]=>
string(3) "132"
}
[6]=>
array(3) {
["id"]=>
string(3) "963"
["x"]=>
string(2) "38"
["y"]=>
string(3) "134"
}
[7]=>
array(3) {
["id"]=>
string(4) "1231"
["x"]=>
string(2) "50"
["y"]=>
string(3) "199"
}
}
我很擅长制作图形插图,但生病了。 这是我的地图: map http://imagizer.imageshack.us/a/img633/3521/E0HGRe.png
我需要访问所有的黑点。 这是我目前的路径:
这条路径不是很优秀.. 这是我想要的路径:
这就是排序功能试图找到的,这是访问所有黑点的最短路径。
答案 0 :(得分:0)
距离计算你应该使用毕达哥拉斯&#39;定理(或只是hypot
,如@prodigitalson提到的那样)。对于地理坐标,您可以使用:http://www.movable-type.co.uk/scripts/latlong.html
让我们修复您的代码。第一个循环的每一步都应该从输入值中取一个值并将其放入结果中。但它继续在里面,因此第一循环的一些步骤不能完成它们的工作。尝试删除第一个继续。
$list_xy = array(
0 => array(
'id' => '308',
'x' => '37',
'y' => '63'
),
1 => array(
'id' => '963',
'x' => '38',
'y' => '134'
),
2 => array(
'id' => '385',
'x' => '39',
'y' => '132'
),
3 => array(
'id' => '1231',
'x' => '50',
'y' => '199'
),
4 => array(
'id' => '788',
'x' => '51',
'y' => '59'
),
5 => array(
'id' => '1151',
'x' => '53',
'y' => '61'
),
6 => array(
'id' => '671',
'x' => '55',
'y' => '60'
),
7 => array(
'id' => '1487',
'x' => '55',
'y' => '55'
)
);
$sorted_list_xy = sort_by_xy_distance($list_xy);
$sorted_list_xy_size = count($sorted_list_xy, COUNT_NORMAL);
$list_xy_size = count($list_xy, COUNT_NORMAL);
var_dump($sorted_list_xy_size == $list_xy_size ? "looks right" : "something is wrong", $sorted_list_xy_size, $list_xy_size);
die("died");
function sort_by_xy_distance($input_list)
{
$ret = array();
$a = $input_list[0];
array_push($ret, $input_list[0]);
$input_list[0] = null;
$i = 1;
for ($i = 1; $i < count($input_list); ++$i) {
// up here
// if ($input_list[$i] == null) {
// echo 'already added to list..';
// continue;
// }
$ii = 1;
$tmpdistance = 0;
$nearest = array(
'index' => -1,
'distance' => PHP_INT_MAX
);
for ($ii = 1; $ii < count($input_list); ++$ii) {
if ($input_list[$ii] == null || $ii == $i) {
//echo 'already added to list..';
continue;
}
$tmpdistance = abs($input_list[$ii]['x'] - $a['x']) + abs($input_list[$ii]['y'] - $a['y']);
if ($tmpdistance < $nearest['distance']) {
$nearest['index'] = $ii;
$nearest['distance'] = $tmpdistance;
}
}
assert($nearest['index'] != -1);
array_push($ret, $input_list[$nearest['index']]);
$a = $input_list[$nearest['index']];
$input_list[$nearest['index']] = null;
}
return $ret;
}