On Fri, 18 Nov 2016 12:00:12 -0800 (PST), Shivu <
rout....@gmail.com>
wrote:
Do you really need to store all of that data?
To see if I understand what you're trying to do, let's walk through
this using pedestrians and vehicles numbered starting at 1. I know
that array indices starting at zero are natural for us programmers,
but if you're going to interface with normal people, it's better to
start at 1.
For each vehicle, store the ID of the closest pedestrian and the
distance to that pedestrian. You could use an array (call it va), and
you could initialize each element to pedestrian zero and a very large
distance:
va[i].p = 0;
va[i].d = 100000000;
for each vehicle ID i.
When you get a report that pedestrian p is distance d from vehicle v,
see if va[v].d < d. If it is, set va[v].d to d and set va[v].p to p.
If va[v].d >= d, do nothing.
When you have to do your periodic report to the pedestrians, loop
through va and tell whoever needs to know that vehicle v is distance d
from pedestrian p.
As Ben said, you should think about what to do if two pedestrians are
the same distance from a vehicle and all other pedestrians are farther
away. I don't have a really good answer to that except to say that if
this is a real-world application in which vehicles will be driven to
pick up the closest pedestrian you'd want to make sure that
pedestrians with, say, higher ID numbers don't consistently get
preempted by people with lower IDs.
If you just pick the first pedestrian to report in case of a tie, or
if you pick the one with the most recent report, I think you should
add a comment to reflect the fact that's it's a conscious decision.
I have to admit that dealing with a tie isn't something I would have
thought of.
Louis