'trying to calculate distance between points, keep getting incorrect output
import math
point_dist = 0.0
x1 = float(input())
y1 = float(input())
x2 = float(input())
y2 = float(input())
point_dist = math.pow(math.sqrt(x2 - x1) + (y2 - y1), 2.0)
print('Points distance:', point_dist)
here is what I have written so far, keeps giving me incorrect numbers for output
input: 1.0, 2.0, 1.0, 5.0
expected output: 9.0
what im getting: 3.0
Solution 1:[1]
math.dist can do this for you.
import math
point_dist = 0.0
x1 = float(input())
y1 = float(input())
x2 = float(input())
y2 = float(input())
point_dist = math.dist((x1, y1), (x2, y2))
print('Points distance:', point_dist)
Note that the distance between (1, 2) and (1, 5) is 3.
Solution 2:[2]
Use the correct formula for distance:
point_dist = ((x2 - x1)**2 + (y2 - y1)**2)**0.5
Solution 3:[3]
Your formula is simply wrong.
Should be changed like this:
point_dist = math.sqrt(math.pow(x2 - x1, 2) + math.pow(y2 - y1, 2))
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | Timur Shtatland |
| Solution 3 | doruksahin |
