Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is point A nearby point B in 3D - distance check

I am looking for efficent algorithm for checking if one point is nearby another in 3D.

sqrt((x2-x1)^2 + (y2-y1)^2 + (z2-z1)^2) < radius

This doesn't seem to be too fast and actually I don't need such a big accuracy. How else could I do this?

like image 519
kjagiello Avatar asked Nov 27 '22 23:11

kjagiello


1 Answers

Square the distance, and drop the call to sqrt(), that's much faster:

(((x2-x1)^2 + (y2-y1)^2 + (z2-z1)^2 < radius * radius

Of course, in many cases at least radius * radius can be computed ahead of time and stored as e.g. squaredRadius.

like image 79
unwind Avatar answered Dec 15 '22 14:12

unwind