比较 3 个位置之间的距离,但结果不可靠
Compare distance between 3 location but the results are not reliable
我正在尝试创建一个代码来计算 2 点之间的距离。我有 3 个点(Point1、Point2 和 Point3),我想比较每个点组合(1 vs 2、1 vs 3 和 2 vs 3)并找出哪个组合与预期结果的距离最长:
The distance between PointX and PointY is the longest, with distance {distance_XY}
我使用的代码是:
def longest_distance(X,Y,Z):
a = X[0]
b = X[1]
c = Y[0]
d = Y[1]
e = Z[0]
f = Z[1]
distance_XY = (((a-c)**2) + ((b-d)**2)) ** (1/2)
distance_XZ = (((a-e)**2) + ((b-f)**2)) ** (1/2)
distance_YZ = (((c-e)**2) + ((d-f)**2)) ** (1/2)
if distance_XY > distance_XZ > distance_YZ:
print(f'The distance between PointX and PointY is the longest, with distance {distance_XY}')
elif distance_XY < distance_XZ > distance_YZ:
print(f'The distance between PointX and PointZ is the longest, with distance {distance_XZ}')
elif distance_XY < distance_XZ < distance_YZ:
print(f'The distance between PointY and PointZ is the longest, with distance {distance_YZ}')
但该代码仅适用于某些数字。当我尝试使用下面的数字时,它起作用了:
X = [0,0]
Y = [5,5]
Z = [10,10]
longest_distance(X,Y,Z)
The distance between PointX and PointZ is the longest, with distance 14.142135623730951
当我尝试使用不同的数字时:
X = [0,0]
Y = [10,-10]
Z = [4,-3]
longest_distance(X,Y,Z)
结果应该是The distance between PointX and PointY is the longest, with distance 14.142135623730951
,但是什么也没发生,也没有错误,所以我真的不知道如何更正它
没有任何反应,因为 None 您设置的条件得到满足。
使用您的输入时:
X = [0,0]
Y = [10,-10]
Z = [4,-3]
发生的事情是:
distance_XY = 14.14213...
distance_XZ = 5.0
distance_YZ = 9.219544...
我们可以清楚地看到 'distance_XY' 是最大的,但是三个条件都为 False 因为:
14.14213... > 5.0 < 9.219544
因此您没有得到任何印刷品。放置 else: 是一个很好的做法,然后写一个一般说明,说明满足 none 个条件。
不过,我会尝试通过执行以下操作来检查最大值:
max_val = max(distance_XY, distance_XZ, distance_YZ)
if distance_XY == max_val:
print(f'The distance between PointX and PointY is the longest, with distance {distance_XY}')
elif distance_XZ == max_val:
print(f'The distance between PointX and PointZ is the longest, with distance {distance_XZ}')
elif distance_YZ == max_val:
print(f'The distance between PointY and PointZ is the longest, with distance {distance_YZ}')
else:
print('None of the conditions were met, something went wrong')
您正在寻找三个值中的最大值,但您要检查的是顺序。在你的第二个例子中:
distance_XY, distance_XZ, distance_YZ = 14.142135623730951 5.0 9.219544457292887
.
这意味着您的第一个条件应该为真。但它不是,因为 distance_XY > distance_XZ -> True
(没关系),而是 distance_XZ > distance_YZ -> False
。所以第二个 >
导致整个条件变为假,即使这不相关,因为 distance_XY
是最大值。
尝试以不同方式定义您的条件以获得最大距离。或者使用另一种最大方法。
通常,要调试此类问题,请尝试添加一些打印语句以查看中间值。然后尝试考虑给定这些值会出现什么问题。
对于使用最大值的简洁方法,您可以执行以下操作:
def longest_distance(X, Y, Z):
(a, b), (c, d), (e, f) = X, Y, Z
distances = {("X", "Y"): (((a-c)**2) + ((b-d)**2)) ** (1/2),
("X", "Z"): (((a-e)**2) + ((b-f)**2)) ** (1/2),
("Y", "Z"): (((c-e)**2) + ((d-f)**2)) ** (1/2)}
(max_name_x, max_name_y), max_value = max(distances.items(),
key=lambda x: x[1])
print(f'The distance between Point{max_name_x} and Point{max_name_y} '
f'is the longest, with distance {max_value}')
我正在尝试创建一个代码来计算 2 点之间的距离。我有 3 个点(Point1、Point2 和 Point3),我想比较每个点组合(1 vs 2、1 vs 3 和 2 vs 3)并找出哪个组合与预期结果的距离最长:
The distance between PointX and PointY is the longest, with distance {distance_XY}
我使用的代码是:
def longest_distance(X,Y,Z):
a = X[0]
b = X[1]
c = Y[0]
d = Y[1]
e = Z[0]
f = Z[1]
distance_XY = (((a-c)**2) + ((b-d)**2)) ** (1/2)
distance_XZ = (((a-e)**2) + ((b-f)**2)) ** (1/2)
distance_YZ = (((c-e)**2) + ((d-f)**2)) ** (1/2)
if distance_XY > distance_XZ > distance_YZ:
print(f'The distance between PointX and PointY is the longest, with distance {distance_XY}')
elif distance_XY < distance_XZ > distance_YZ:
print(f'The distance between PointX and PointZ is the longest, with distance {distance_XZ}')
elif distance_XY < distance_XZ < distance_YZ:
print(f'The distance between PointY and PointZ is the longest, with distance {distance_YZ}')
但该代码仅适用于某些数字。当我尝试使用下面的数字时,它起作用了:
X = [0,0]
Y = [5,5]
Z = [10,10]
longest_distance(X,Y,Z)
The distance between PointX and PointZ is the longest, with distance 14.142135623730951
当我尝试使用不同的数字时:
X = [0,0]
Y = [10,-10]
Z = [4,-3]
longest_distance(X,Y,Z)
结果应该是The distance between PointX and PointY is the longest, with distance 14.142135623730951
,但是什么也没发生,也没有错误,所以我真的不知道如何更正它
没有任何反应,因为 None 您设置的条件得到满足。 使用您的输入时:
X = [0,0]
Y = [10,-10]
Z = [4,-3]
发生的事情是:
distance_XY = 14.14213...
distance_XZ = 5.0
distance_YZ = 9.219544...
我们可以清楚地看到 'distance_XY' 是最大的,但是三个条件都为 False 因为: 14.14213... > 5.0 < 9.219544
因此您没有得到任何印刷品。放置 else: 是一个很好的做法,然后写一个一般说明,说明满足 none 个条件。
不过,我会尝试通过执行以下操作来检查最大值:
max_val = max(distance_XY, distance_XZ, distance_YZ)
if distance_XY == max_val:
print(f'The distance between PointX and PointY is the longest, with distance {distance_XY}')
elif distance_XZ == max_val:
print(f'The distance between PointX and PointZ is the longest, with distance {distance_XZ}')
elif distance_YZ == max_val:
print(f'The distance between PointY and PointZ is the longest, with distance {distance_YZ}')
else:
print('None of the conditions were met, something went wrong')
您正在寻找三个值中的最大值,但您要检查的是顺序。在你的第二个例子中:
distance_XY, distance_XZ, distance_YZ = 14.142135623730951 5.0 9.219544457292887
.
这意味着您的第一个条件应该为真。但它不是,因为 distance_XY > distance_XZ -> True
(没关系),而是 distance_XZ > distance_YZ -> False
。所以第二个 >
导致整个条件变为假,即使这不相关,因为 distance_XY
是最大值。
尝试以不同方式定义您的条件以获得最大距离。或者使用另一种最大方法。
通常,要调试此类问题,请尝试添加一些打印语句以查看中间值。然后尝试考虑给定这些值会出现什么问题。
对于使用最大值的简洁方法,您可以执行以下操作:
def longest_distance(X, Y, Z):
(a, b), (c, d), (e, f) = X, Y, Z
distances = {("X", "Y"): (((a-c)**2) + ((b-d)**2)) ** (1/2),
("X", "Z"): (((a-e)**2) + ((b-f)**2)) ** (1/2),
("Y", "Z"): (((c-e)**2) + ((d-f)**2)) ** (1/2)}
(max_name_x, max_name_y), max_value = max(distances.items(),
key=lambda x: x[1])
print(f'The distance between Point{max_name_x} and Point{max_name_y} '
f'is the longest, with distance {max_value}')