2

我有一个 X 和 y 坐标数组,我怎样才能找到离 0,0 最近的点并从离 0,0 的最近到最远的点对它们进行排序?

  <?php
        $array = array(array("x" => 10,
                             "y" => 10),

                       array("x" => 120,
                             "y" => 560),

                       array("x" => 950,
                             "y" => 23),

                       array("x" => 78,
                             "y" => 40),);
    ?>

提前感谢,对不起我的英语:|

4

5 回答 5

1

使用 usort:

<?php
//your array
$array = array(array("x" => 10,
                     "y" => 10),

               array("x" => 120,
                     "y" => 560),

               array("x" => 950,
                     "y" => 23),

               array("x" => 78,
                     "y" => 40),);

//define a compare function
function cmp($a,$b){
    //get the squared distance of a and b
    $distA_SQ = $a['x']*$a['x']+$a['y']*$a['y'];
    $distB_SQ = $b['x']*$b['x']+$b['y']*$b['y'];

    //if squared distances are the same, return 0
    if($distA_SQ==$distB_SQ)return 0;

    //distances are not the same so return 1 if a larger than b or -1 if b larger than a
    return $distA_SQ>$distB_SQ?1:-1;
}

//run the sort function
usort($array, 'cmp');

//output the array
var_dump($array);

http://codepad.org/OBH1cskb

并且要确定点 A 的距离是否大于 B,您不需要计算距离。这是昂贵且不必要的。

编辑:在下面的代码和解释中添加了注释

这使用usort,它使用用户定义的比较函数。usort 将通过调用您的比较函数并一次传入两个值(通常作为 $a 和 $b 传入)来查看执行快速排序的数组,并希望您的比较函数在 $a 小于 $b 时返回 -1 , 如果 $a 等于 $b,则为 0,如果 $a 大于 $b,则为 1。您可以在手册中阅读有关 usort 的更多信息。

于 2013-03-22T16:37:41.473 回答
0

创建一个新数组并将 x、y 及其与 (0,0) 的距离放入其中。

$distArray = array();
foreach($distArray as $point):
$distArray[] = array($point['x'],$point['y'],dist($point['x'],$point['y']));
endforeach;

现在按该数组的第三个元素对该数组进行排序。我相信编写 dist() 函数会很容易。

编辑:我建议将 x 和 y 保留在数组中,这样当您对结果数组进行排序时,您就知道哪个项目是哪个点。

于 2013-03-22T16:16:36.807 回答
0

查看http://www.ltcconline.net/greenl/courses/154/factor/circle.htm

function calculateDistance($x,$y)
{
    //apply formula
    return sqrt(pow($x, 2) + pow($y, 2)); //<--Sammitch pointed out directly 
}
$data = array();
foreach($array as $key=>$distance)
{//this is better because you can have points that have the same distance
   $data[calculateDistance($distance['x'],$distance['y'])][] = $key;
}
    ksort($data);

结果

 in:
$array = array(array("x" => 10,
                             "y" => 10),

                       array("x" => 120,
                             "y" => 560),
                       array("x" => 120,
                             "y" => 560),
                       array("x" => 950,
                             "y" => 23),

                       array("x" => 78,
                             "y" => 40));
    output:
    array (size=4)
      14 =>  //<--key is the distance and the value are the keys from your array
        array (size=1)
          0 => int 0
      87 => 
        array (size=1)
          0 => int 4
      572 => 
        array (size=2)
          0 => int 1
          1 => int 2
      950 => 
        array (size=1)
          0 => int 3
于 2013-03-22T16:16:59.067 回答
0

试试这个

<?php
$array = array(array("x" => 10,
                     "y" => 10),

               array("x" => 120,
                     "y" => 560),

               array("x" => 950,
                     "y" => 23),

               array("x" => 78,
                     "y" => 40),);


$distance = array();
 $req_array = array();
 foreach($array as $subArray)
{
 $distance[] = sqrt(pow(($subArray[x],2)+pow(($subArray[y],2));
}

asort($distance);

foreach($distance as $key=>$value)
{
  $req_array[] = $array[$key];
}


print_r($distance);
print_r($req_array);

?>
于 2013-03-22T16:18:13.157 回答
0
$array = array(
    array("x" => 10, "y" => 10),

    array("x" => 120, "y" => 560),

    array("x" => 950, "y" => 23),

    array("x" => 78, "y" => 40)
);

$start_point_array = $array;
$end_point_array = $array;
$farthest_points = array();
$farthest_distance = 0;

foreach($start_point_array as $index => $start_point) {
    for($i = $index + 1; $i < count($end_point_array); $i++) {
        $end_point = $end_point_array[$i];
        $distance = sqrt(pow($end_point['x'] - $start_point['x'], 2) + pow($end_point['y'] - $start_point['y'], 2));
        if($distance > $farthest_distance) {
            $farthest_distance = $distance;
            $farthest_points = array($start_point, $end_point); // Or whatever
        }
    }
}
于 2013-03-22T16:25:27.303 回答