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
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.
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.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With