0

I need to solve this kind of problem :

I hava a list of many objects, each object has his own [x][y], and I need to find the closest object to object A from objects class B.

I know this is maybe a bad explanation but I am doing my best.

Pseudo Code :
List<Object> myList
myList.add(new C) x,y not important
myList.add(new B)
myList.add(new C) x,y not important
myList.add(new A)
myList.add(new C) x,y not important
myList.add(new B)
myList.add(new A)
myList.add(new C) x,y not important
myList.add(new E) x,y not important
myList.add(new E) x,y not important
myList.add(new A)
...
The result of searching should be something like this :
A x,y is the closest to B x,y
A x,y is the closest to B x,y 
A x,y is the closest to B x,y

The second thing I need to do is to generate path to do the closest ones.

I don't ask you to 'ready code' just maybe a little advice, some articular ...

My first idea was to create a new List witch will contain only Objects A, after that maybe sort it, and later find somehow the closest one, but I don't know how.

If you have any idea please help, Thank you very much!

Eken
  • 69
  • 8
  • Do you know about the [distance formula](http://www.purplemath.com/modules/distform.htm)? – River May 14 '17 at 23:28
  • How are you sorting your objects? By x coordinate? By y coordinate? If you think about it, you'll see that "sorting" according to these two parameters doesn't help you discover the object with the closest Euclidean distance to your target object A. – NoseKnowsAll May 14 '17 at 23:29
  • Is this related ? http://stackoverflow.com/questions/43833884/algorithm-for-pathfinding-in-2d-array?noredirect=1#comment74722851_43833884 – esQmo_ May 14 '17 at 23:32
  • The first part of your question looks like a duplicate. I'm not clear on what the second part means (a path from _what_ to the closest objects?), but it might be answerable by the link esQmo_ provided, or you might want to look into Dijkstra's shortest path algorithm. In any case, it should probably be a separate question. – ajb May 15 '17 at 00:10
  • @ajb why duplicate ? – Yahya May 15 '17 at 00:11
  • @Yahya because it appears to be a question someone already asked and answered. Do you have a reason for thinking it's not a duplicate? – ajb May 15 '17 at 00:12
  • @ajb IK what the meaning of duplicate , but where is the duplicate question (the link)? – Yahya May 15 '17 at 00:14
  • @Yahya at the top of the page – ajb May 15 '17 at 00:15
  • @esQmo yes thank you – Eken May 15 '17 at 13:46

0 Answers0