简体   繁体   中英

find lowest x and y

I have an array of X and y coordinates, how can I find the nearest point to the 0,0 and sort them from the nearest to the farthest point from the 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),);
    ?>

Thanks in advance and sorry for my english :|

Using 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

And to determine if distance of point A is greater than B, you don't need to sqrt the distance. It is expensive and unnecessary.

Edit: Added comments to code and explanation below

This uses usort , which uses a user defined compare function. usort will look over the array performing a quicksort by calling your compare function and passing in two values at a time (usually passed in as $a and $b) and expects your compare function to return -1 if $a is less than $b, 0 if $a is equal to $b or 1 if $a is greater than $b. you can read more on usort in the manual .

Create a new array and put x, y and their distance from (0,0) in it.

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

now sort this array by it's third element. I believe writing the dist() function would be easy.

EDIT: I suggest to keep the x and y in your array so when you sort the result array, you know which item is which point.

Check out 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

try this

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM