[help] Calculate 10 closest objects from an array
#1

I need some help with calculating the 10 closest objects from an array..
I know how to calculate THE closest object but not how to calculate the 10 closest :l
I don't need to get code for it, but just a little help so I know how I need to create it

thx
Reply
#2

Quote:
Originally Posted by °ғαιιοцт°
I know how to calculate THE closest object but not how to calculate the 10 closest :l
Can you post that one please? Maybe I can help you.
Reply
#3

Quote:
Originally Posted by » RэРиR «
Quote:
Originally Posted by °ғαιιοцт°
I know how to calculate THE closest object but not how to calculate the 10 closest :l
Can you post that one please? Maybe I can help you.
loop trough all coordinates, and calculate the distance

if the distance is smaller than the previous distance, that object is closer
once you've looped trough all objects, the smallest one will be left :P (smallest distance)

here is some code for creating a closest object, it's from somewhere in my script :P for calculating closest objects

Код:
				new closest;
			  new dist = -1;
			  for(new o=0; o<21; o++)
			  {
			    new dist2 = distance(i, ClosestPlatform[o][0], ClosestPlatform[o][1], ClosestPlatform[o][2]);
			    if(dist2 < dist || dist == -1)
			    {
			      closest = o;
			      dist = dist2;
			    }
			  }
Reply
#4

Find the closest one 10 times (loop), but every time you find one, save it to a variable and skip it on next check.
Reply
#5

Quote:
Originally Posted by MadeMan
Find the closest one 10 times (loop), but every time you find one, save it to a variable and skip it on next check.
I know.. but looping 10 times isn't an option
I want it with looping just once

it must be possible, looping 10 times is reading every coordinate 9 times too much
Reply
#6

Same as getting highest ranked player, you have to loop many times through, whilst excluding the highest rank each time.
Although it's a lot of looping, it's only looping through an array, I wouldn't worry about it.






Reply
#7

Quote:
Originally Posted by Rac3r
Same as getting highest ranked player, you have to loop many times through, whilst excluding the highest rank each time.
Although it's a lot of looping, it's only looping through an array, I wouldn't worry about it.






You loop to get information about each object, in this case the Distance
why calculate the distance 10 times if you already know it from the first time?
there must be a way with only looping 1 time

the way you would do it:

- Loop trough: 500 players
- 250 closest objects
- loop trough all objects (possibly 10.000)

10.000*250*500 = 1.250.000 times ...
and that more than once/second ??
that is instant CPU death I guess
Reply
#8

Quote:
Originally Posted by °ғαιιοцт°
Quote:
Originally Posted by MadeMan
Find the closest one 10 times (loop), but every time you find one, save it to a variable and skip it on next check.
I know.. but looping 10 times isn't an option
I want it with looping just once

it must be possible, looping 10 times is reading every coordinate 9 times too much
You can calculate the distances only once, save them to an array and then loop them.

Other option is to create an array with 10 slots, save 10 first distances to this array and then every time find the longest distance from that array and replace it with smaller. This way you will loop 10 items, but you have to do it on every object.
Reply
#9

I use this loop to loop through players in race, so I edited the loop for you.
if you're going to make a repeating timer to loop, you must make ObjectPos a
global variable, otherwise it can be in the function, where you loop.

pawn Код:
new ObjectPos[MAX_OBJECTS] = { MAX_OBJECTS, ... };
new Float:odis[MAX_OBJECTS];
new Float:sdis[10] = { 30000.00, ... };
for(new i; i < MAX_OBJECTS; i++)
{
  odis[i] = GetObjectDistance(...);
  for(new j; j < 10; j++)
  {
    if(odis[i] < sdis[j])
    {
      for(new o = ObjectPos[i]; o > j; o--)
      {
        sdis[o] = sdis[o-1];
        ObjectPos[o] = ObjectPos[o-1];
      }
      ObjectPos[i] = j;
      sdis[j] = odis[i];
      break;
    }
  }
}
(Note: Its not tested)
Reply
#10

Quote:
Originally Posted by Mастерминд
I use this loop to loop through players in race, so I edited the loop for you.
if you're going to make a repeating timer to loop, you must make ObjectPos a
global variable, otherwise it can be in the function, where you loop.

pawn Код:
new ObjectPos[MAX_OBJECTS] = { MAX_OBJECTS, ... };
new Float:odis[MAX_OBJECTS];
new Float:sdis[10] = { 30000.00, ... };
for(new i; i < MAX_OBJECTS; i++)
{
  odis[i] = GetObjectDistance(...);
  for(new j; j < 10; j++)
  {
    if(odis[i] < sdis[j])
    {
      for(new o = ObjectPos[i]; o > j; o--)
      {
        sdis[o] = sdis[o-1];
        ObjectPos[o] = ObjectPos[o-1];
      }
      ObjectPos[i] = j;
      sdis[j] = odis[i];
      break;
    }
  }
}
(Note: Its not tested)
but this gives me the 10 smallest Distances
I need the 10 closest object id's :l
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)