Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Algorithm for calculating a distance between 2 3-dimensional points?

Tags:

c#

I have two 3-D points.
Example:

float[] point1 = new float[3] {1.3919023, 6.12837912, 10.391283};
float[] point2 = new float[3] {48.3818, 38.38182, 318.381823};

Anyone an idea for an algorithm to calculate the distance in float between the points?

like image 532
Headpuster Avatar asked Jan 18 '12 17:01

Headpuster


People also ask

How do you find the distance between two points in an algorithm?

Solution: The distance between two points using coordinates can be given as, d = √[(x2 x 2 − x1 x 1 )2 + (y2 y 2 − y1 y 1 )2], where (x1,y1 x 1 , y 1 ) and (x2,y2 x 2 , y 2 ) are the coordinates of the two points.

How do you find the distance between two dimensions?

What is 2D Distance Formula? The 2D distance formula gives the shortest distance between two points in a two-dimensional plane. The formula says the distance between two points (x1,y1) ( x 1 , y 1 ) , and (x2,y2) ( x 2 , y 2 ) is D=√(x2−x1)2+(y2−y1)2 D = ( x 2 − x 1 ) 2 + ( y 2 − y 1 ) 2 .

What is 3D distance formula?

What Is 3D Distance Formula? The 3d distance formula says, the distance between two points in the space A(x1, y1, z1) and B(x2, y2, z2) is given as: d=√(x2−x1)2+(y2−y1)2+(z2−z1)2.

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

To calculate the distance between two points in 3D in Python, use the math. dist() method.


1 Answers

The Euclidean distance between two 3D points is:

float deltaX = x1 - x0;
float deltaY = y1 - y0;
float deltaZ = z1 - z0;

float distance = (float) Math.Sqrt(deltaX * deltaX + deltaY * deltaY + deltaZ * deltaZ);

And in N dimensions (untested and vulnerable to overflow):

float DistanceN(float[] first, float[] second) {
  var sum = first.Select((x, i) => (x - second[i]) * (x - second[i])).Sum();
  return Math.Sqrt(sum);
}

Edit: I much prefer the Zip solution posted by dasblinkenlight below!

like image 112
Ron Warholic Avatar answered Oct 06 '22 00:10

Ron Warholic