View Single Post
10-26-14, 12:16 PM   #2
semlar
A Pyroguard Emberseer
 
semlar's Avatar
AddOn Author - Click to view addons
Join Date: Sep 2007
Posts: 1,060
Originally Posted by mekasha View Post
My question is, is there any way to kind of sort the items based on a relative closeness to player position? I don't need to find "distance", I was just thinking something simple like ((player X coord - item X coord) or (item X coord - player X coord)) based on whichever is higher, then sorting based on the lower result.
Distance is pretty much exactly what you're trying to sort by.

If you have a table like
Lua Code:
  1. {
  2.   {34132, 76.5, 63.5},
  3.   {34931, 21.7, 50.8},
  4.   {34133, 26.9, 31.9},
  5. }
And you only want to sort by the X coordinate, you would get the player's coordinates and then sort the table..
Lua Code:
  1. local coords = {
  2.   {34132, 76.5, 63.5},
  3.   {34931, 21.7, 50.8},
  4.   {34133, 26.9, 31.9},
  5. }
  6. local playerX, playerY = GetPlayerMapPosition('player')
  7. table.sort(coords, function(a, b)
  8.   return (a[2] - playerX) < (b[2] - playerX)
  9. end)
For actual distance you'd sort like this
Lua Code:
  1. table.sort(coords, function(a, b)
  2.   return ((a[2] - playerX)^2 + (a[3] - playerY)^2) < ((b[2] - playerX)^2 + (b[3] - playerY)^2)
  3. end)

Last edited by semlar : 10-26-14 at 12:44 PM.
  Reply With Quote