0

enter image description here

Assuming I have the above two sets of objects (of class A and B)... how can I efficiently pair the objects in set A with the closest unique object from set B where that object is not closer to any other set A object.

The objects in both sets use 2D vectors to represent their positions.

sookie
  • 2,437
  • 3
  • 29
  • 48
  • What happens when the closest object from B, to an object from A is closer to another object from A? So the closest B to *a1* is *b1*, but the closest A to *b1* is *a2*? – jaggedSpire Jan 16 '17 at 16:40
  • @jaggedSpire: *a2* would be paired with *b1* – sookie Jan 16 '17 at 16:47
  • And *a1* would be paired with nothing? – jaggedSpire Jan 16 '17 at 16:47
  • @jaggedSpire: *a1* would be paired with the next closest B, assuming it isn't closer to another unique A object – sookie Jan 16 '17 at 16:48
  • so your problem can additionally be stated like so: For each *a* from set A, and each unique *b* from set B where *a* is the closest member of A, pair *a* with the closest *b*. – jaggedSpire Jan 16 '17 at 16:51
  • @jaggedSpire: Yep that's correct – sookie Jan 16 '17 at 16:53
  • In that case, I'd get the set C of unique values of B, for each C find the closest member of A, and find the *c* with the minimum distance to each *a*, using only those *c* which are closer to *a* than any other member of A. – jaggedSpire Jan 16 '17 at 17:01
  • Potential areas with variable performance would be finding the closest *a* for each *c*, and exactly how you're finding which *c* has the minimum distance there: you could store the candidates for each *a* in a data structure that you alter as you iterate over the elements of *c* to find their closest *a*, for example. – jaggedSpire Jan 16 '17 at 17:04
  • The mechanism for finding the closest point is a *point* (heh) that can see a [number of optimizations.](http://stackoverflow.com/questions/1901139/closest-point-to-a-given-point). Additionally, since you're trying to do this for every *a* with a valid result, you might want to be careful with the data structure you use to store the record of the minimum case. You might want to use a hash table, or even a vector if you have a way to quickly map each element in *a* to an index or if you can simply retain that information while you're accessing *a* for the distance computation. – jaggedSpire Jan 16 '17 at 17:15

1 Answers1

0

One scheme for arranging 2D lists by proximity involves Peano encoding the entries, which basically amounts to a perfect bit shuffle of each (x,y) coordinate of n-bits into a single interleaved 2n-bit value. (I first learned of this scheme reading about US Census tract mapping and TIGER data ca. 1984.)

Ex: (x,y)=(0x4,0xB)=(0_1_0_0,_1_1_0_1), then Peano(x,y) is 01110001 = 71.

This bit shuffle results in a list of numbers that, when sorted, represent 2D proximity along a single number line. It is easy to show that for Peano values A,B,C that A < B < C implies B lies in a 2-D rectangle bounded by A and C.

Just a thought, and may not be what you need at all. Good luck.

  • I'm not sure how applicable this is as the object positions are represented using floats. Also as this is a real-time application, I'm uncertain what the cost of encoding might be. – sookie Jan 16 '17 at 16:57