Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Efficient way of finding distance between two 3D points

I am writing a code in C++ and want to compute distance between two points. Question 1:

I have two points P(x1, y1, z1) and Q(x2, y2, z2) , where x, y and z are floats/doubles.

I want to find the distance between these two points. One way to do it is :

square_root(x_diffx_diff + y_diffy_diff + z_diff*z_diff)

But this is probably not the most efficient way . (e.g. a better formula or a ready made utility in math.h etc )

Question 2:

Is there a better way if I just want to determine if P and Q are in fact the same points?

My inputs are x, y and z coordinates of both the points.

Thank you

like image 520
memC Avatar asked Feb 15 '10 08:02

memC


People also ask

How do you find the distance between two points in 3D?

To find the distance between two points, take the coordinates of two points such as (x1, y1) and (x2, y2) Use the distance formula (i.e) square root of (x2 – x1)2 + (y2 – y1) For this formula, calculate the horizontal and vertical distance between two points.

What is 3D distance formula?

You know that the distance AB between two points in a plane with Cartesian coordinates A(x1,y1) and B(x2,y2) is given by the following formula: AB=√(x2−x1)2+(y2−y1)2.


1 Answers

Do you need the actual distance? You could use the distance squared to determine if they are the same, and for many other purposes. (saves on the sqrt operation)

like image 153
James Avatar answered Sep 21 '22 13:09

James