【问题标题】:I have coordinates pool with x,y combniation I need to match nearest match of given x,y我有带有 x,y 组合器的坐标池我需要匹配给定 x,y 的最接近匹配
【发布时间】:2017-10-03 19:22:01
【问题描述】:

例子

1   8.919280,45.622651  
2   8.910296,45.626021  
3   8.914084,45.627028  
4   8.913913,45.62941  

我必须匹配 x,y 不是 x 和 y 的总和,而是 x 和 y 最接近的。 不是最接近 (x+y) 或 ((x1-x)+(y1-y)) 的都是错误的,我需要完美匹配。

8.912680,45.629392

foreach($pair_coordinates as $pair_coordinate)
                    {
                    sscanf((string)$pair_coordinate, '%f,%f',$longitude, $latitude);
                        $data[$move]['lng'] = $longitude;
                        $data[$move]['lat'] = $latitude;
                        $data[$move]['longitude'] = ($longitude-$_POST['k_lng']);
                        $data[$move]['latitude'] = ($latitude-$_POST['k_lat']);

                        $data[$move]['diff'] = $data[$move]['longitude'] + $data[$move]['latitude'];

                        $data[$move]['diff'] = abs($data[$move]['diff']);

                        $sort[$move] = $data[$move]['diff'];
                        $sort_old[$move] = $data[$move]['diff'];

                        $data[$move]['zona'] = (string)$placemarker->ExtendedData->Data[2]->value;
                        $data[$move]['codcom'] = (string)$placemarker->ExtendedData->Data[1]->value;
                        $data[$move]['markername'] = (string)$placemarker->name;
                        if($data[$move]['longitude'] < 0 || $data[$move]['latitude'] < 0)
                        {
                            unset($sort[$move]);
                        }


                        $move++;
                    }







                    asort($sort);

【问题讨论】:

  • 我们总是乐于帮助和支持新的编码员,但您需要先帮助自己。 :-)doing more research 之后,如果您有问题发布您尝试过的方法,并清楚地解释什么不起作用并提供a Minimal, Complete, and Verifiable example。阅读How to Ask 一个好问题。请务必take the tour 并阅读this
  • like 值对需要与值对数组匹配。
  • 您要解决斜边问题吗?
  • 是的,我的问题非常棘手,我从过去 9 年开始一直在编码,对于简单的编码人员和简单的解决方案制造商来说,这很难理解。
  • 循环槽并计算每个的 squareRoot(abs(X1-X2)^2)+abs(Y1-Y2)^2),最小的将是最接近的

标签: php algorithm sorting coordinates coordinate-systems


【解决方案1】:

也许这会帮助你走上正轨:

<?php

$coords = array(
    array("lng" => 8.919280, "lat" => 45.622651),
    array("lng" => 8.910296, "lat" => 45.626021),
    array("lng" => 8.914084, "lat" => 45.627028),
    array("lng" => 8.913913, "lat" => 45.62941)
);

$find = array("lng" => 8.912680, "lat" => 45.629392);

$idx = null;
$shortest = null;

foreach($coords as $k => $v)
{
    // I know the abs() calls are optional, but this allows for some other logic to be used on the individual distances aswell.
    $dLng = abs($find["lng"] - $v["lng"]); 
    $dLat = abs($find["lat"] - $v["lat"]);

    $distance = sqrt($dLng * $dLng + $dLat * $dLat);

    if (!$shortest || $distance < $shortest)
    {
        $shortest = $distance;
        $idx = $k;
    }
}

if ($idx)
{
    echo "Found:\n";
    print_r($coords[$idx]);
} else {
    echo "Nothing found.";
}

输出:

Found:
Array
(
    [lng] => 8.913913
    [lat] => 45.62941
)

使用array_reduce的另一种解决方案:

<?php

$coords = array(
    array("lng" => 8.919280, "lat" => 45.622651),
    array("lng" => 8.910296, "lat" => 45.626021),
    array("lng" => 8.914084, "lat" => 45.627028),
    array("lng" => 8.913913, "lat" => 45.62941)
);

$find = array("lng" => 8.912680, "lat" => 45.629392);

function distance($a, $b)
{
    $dLng = abs($a["lng"] - $b["lng"]);
    $dLat = abs($a["lat"] - $b["lat"]);

    $distance = sqrt($dLng * $dLng + $dLat * $dLat);

    return $distance;
}


$nearest = array_reduce($coords, function($carry, $item) use ($find)
{
    if (!$carry)
        return $item;

    $dCarry = distance($find, $carry);
    $dItem = distance($find, $item);

    return $dCarry < $dItem ? $carry : $item;
}, null);

print_r($nearest);

输出:

Array
(
    [lng] => 8.913913
    [lat] => 45.62941
)

【讨论】:

    【解决方案2】:
    $pair_coordinates = array(array(10.919280,45.622651),array(8.910296,45.626021),array(8.914084,45.627028), array(8.913913,45.62941 ));
    
    $i = 0;
    
        $x = 8.919380;
        $y = 45.623651;
    
    foreach($pair_coordinates as $pair_coordinate)
    {
    
    
        $lng = $pair_coordinate[0] - $x;
        $lon = $pair_coordinate[1] - $y;        
    
        $data[$i] = sqrt(pow($lng,2) + pow($lon,2));
    
        $i++;
    }
    
    $m = array_search(min($data), $data);
    
    print_r($pair_coordinates[$m]);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-12
      • 1970-01-01
      • 1970-01-01
      • 2019-07-01
      • 1970-01-01
      • 2015-12-13
      相关资源
      最近更新 更多