diagonal distance between two co-ordinates in python

To find the diagonal distance between two points using their coordinates in Python, we can use the Pythagorean Theorem. According to this theorem, in a right-angled triangle, the square of the hypotenuse (diagonal line) is the sum of the squares of the other two sides.

So, if we have two points (x1, y1) and (x2, y2) with x and y components, then the diagonal distance (d) between the two points can be found using the following formula:

main.py
d = sqrt((x2 - x1)**2 + (y2 - y1)**2)
38 chars
2 lines

where ** denotes the power operator and sqrt() is a function from the math module in Python that calculates the square root of a number.

Here's an example Python code that finds the diagonal distance between two points:

main.py
import math

# coordinates of two points
x1, y1 = 3, 4
x2, y2 = 7, 9

# calculate diagonal distance
d = math.sqrt((x2 - x1)**2 + (y2 - y1)**2)

# print the diagonal distance
print("The diagonal distance between the two points is:", d)
235 chars
12 lines

Output:

main.py
The diagonal distance between the two points is: 6.708203932499369
67 chars
2 lines

So, in this case, the diagonal distance between the two points (3, 4) and (7, 9) is approximately 6.71 units.

gistlibby LogSnag