SOLVED: odering by distance of two points.

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
User avatar
panic!
Forum Regular
Posts: 516
Joined: Mon Jul 31, 2006 7:59 am
Location: Brighton, UK

SOLVED: odering by distance of two points.

Post 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 :)
Last edited by panic! on Tue Sep 19, 2006 4:49 am, edited 1 time in total.
User avatar
panic!
Forum Regular
Posts: 516
Joined: Mon Jul 31, 2006 7:59 am
Location: Brighton, UK

Post by panic! »

ok I ran it again and it worked perfectly with no changes...feel free to use my code...weird.
Post Reply