Page 1 of 1

SOLVED: odering by distance of two points.

Posted: Tue Sep 19, 2006 4:43 am
by panic!
Hello all!

I'm having problems using a mathematical SELECT, I've never actually done one so this could be all totally wrong.

Code: Select all

$x=$panorama['map_x']; // pull out the origin x

$y=$panorama['map_y']; //pull out the origin y

$panorama_rs=mysql_query("select * FROM `pa_panoramas` order by abs(sqrt((($x-`map_x`)*($x-`map_x`))+(($y-`map_y`)*($y-`map_y`)))) ASC");
cue awesome paint diagram:

Image

what I'm doing is simple pythagoras: squaring the difference between the row `map_x` (destination on the diagram) and the origin x and then squaring the row `map_y` and the origin y then finding the square route of the sum of the two numbers and trying to ORDER BYthat number, ie from nearest to furthest.

Any clues would be greatly appreciated :)

Posted: Tue Sep 19, 2006 4:45 am
by panic!
ok I ran it again and it worked perfectly with no changes...feel free to use my code...weird.