找到最低的x和y

时间:2013-03-22 16:10:16

标签: php math

我有一个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),);
    ?>

提前致谢并抱歉我的英文:|

5 个答案:

答案 0 :(得分: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,您不需要对距离进行sqrt。这是昂贵且不必要的。

编辑:为下面的代码和说明添加了注释

这使用usort,它使用用户定义的比较函数。 usort将通过调用compare函数并一次传入两个值(通常以$ a和$ b传递)查看执行快速排序的数组,并期望如果$ a小于$ b,则compare函数返回-1 ,如果$ a等于$ b,则为0;如果$ a大于$ b,则为1。您可以在manual

中阅读更多内容

答案 1 :(得分: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保留在数组中,这样当你对结果数组进行排序时,你知道哪个项是哪个点。

答案 2 :(得分: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);

RESULT

 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

答案 3 :(得分: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);

?>

答案 4 :(得分: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
        }
    }
}