0

How to calculate the distance between points in three-dimensional space? I have three one-dimensional arrays, which are used to build a three-dimensional graph. I need to find the distance between the points. I would like to take the interval along the y axis, sort the points and find the length of the line, then the next interval, etc., and then add everything up. But I can't do it. Please tell me how I can do this or some other way of calculating. using Python

I tried using pdist, dividing the y axis into intervals

Woodford
  • 3,746
  • 1
  • 15
  • 29
lainri
  • 1
  • 1
    Does this answer your question? [Finding 3d distances using an inbuilt function in python](https://stackoverflow.com/questions/20184992/finding-3d-distances-using-an-inbuilt-function-in-python) – Woodford May 15 '23 at 23:06
  • a good question, but i have voted to close because it has been asked before (as per @Woodford comment). – D.L May 15 '23 at 23:20

1 Answers1

2

The following code shows how to use the math.dist() function to calculate the distance between two points in a 3 dimensional space:

import math

# Define the points
p1 = (1, 2, 3)
p2 = (4, 5, 6)

# Calculate the distance
distance = math.dist(p1, p2)

# Print the distance
print(distance)

In this example, the output is 5.

If you want to do this manually (no libraries) you need to use the Pythagorean theorem.

# Define the points
p1 = (1, 2, 3)
p2 = (4, 5, 6)

# Calculate the distance
x1, y1, z1 = p1
x2, y2, z2 = p2

distance = sqrt((x2 - x1)**2 + (y2 - y1)**2 + (z2 - z1)**2)

# Print the distance
print(distance)
D.L
  • 4,339
  • 5
  • 22
  • 45
Roberto Yoc
  • 461
  • 2
  • 7