Fastest way to find the closest point to a given point in 3D, in Python.

Posted by Saebin on Stack Overflow See other posts from Stack Overflow or by Saebin
Published on 2010-04-14T21:31:14Z Indexed on 2010/04/14 21:33 UTC
Read the original article Hit count: 198

Filed under:
|
|
|

So lets say I have 10,000 points in A and 10,000 points in B and want to find out the closest point in A for every B point.

Currently, I simply loop through every point in B and A to find which one is closest in distance. ie.

B = [(.5, 1, 1), (1, .1, 1), (1, 1, .2)]
A = [(1, 1, .3), (1, 0, 1), (.4, 1, 1)]
C = {}
for bp in B:
   closestDist = -1
   for ap in A:
      dist = sum(((bp[0]-ap[0])**2, (bp[1]-ap[1])**2, (bp[2]-ap[2])**2))
      if(closestDist > dist or closestDist == -1):
         C[bp] = ap
         closestDist = dist
print C

However, I am sure there is a faster way to do this... any ideas?

© Stack Overflow or respective owner

Related posts about python

Related posts about points