2013-03-22 72 views
2

我有一個X和Y座標的數組,我怎麼能找到最接近0,0的點,並將它們從0,0中最接近的最遠點排序?找到最低的x和y

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

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

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

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

在此先感謝和抱歉我的英語:

+5

[你有什麼嘗試?](http://www.whathaveyoutried.com/)請參閱[問問建議](http://stackoverflow.com/questions/ask-advice),請。 – 2013-03-22 16:10:50

+0

我同意約翰,請嘗試一下,尋找排序或搜索算法,並嘗試一些實現,如果你沒有找到任何好的解決方案,發佈你做了什麼,並尋求幫助。這是我的建議。 – Rafa 2013-03-22 16:14:15

+0

答案取決於你如何定義距離,請參閱http://en.wikipedia.org/wiki/Distance_metric – 2013-03-22 16:15:15

回答

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。你可以在manual上閱讀更多關於usort的內容。

+0

我沒有得到它的工作原理:|你能解釋一下嗎? – 2013-03-22 16:57:47

+0

@Bamba補充說明。 – 2013-03-22 17:12:14

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在你的數組,所以當你排序結果數組時,你知道哪個項是哪個點。

+0

在你的函數中寫下你自己的https://www.purplemath.com/modules/distform.htm。 – AliBZ 2013-03-22 16:21:44

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 
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); 

?> 
+0

謝謝!有用! – 2013-03-22 16:51:15

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 
     } 
    } 
}