寫一個 Python 程序,求兩點之間的距離。這個 python 示例接受第一個和第二個坐標點,並使用數學冪和 sqrt 函數計算距離。
import math
x1 = int(input("Enter the First Point Coordinate x1 = "))
y1 = int(input("Enter the First Point Coordinate y1 = "))
x2 = int(input("Enter the Second Point Coordinate x2 = "))
y2 = int(input("Enter the Second Point Coordinate y2 = "))
x = math.pow((x2 - x1), 2)
y = math.pow((y2 - y1), 2)
print(x)
print(y)
print(math.sqrt(x + y))
distance = math.sqrt(x + y)
print('The Distance Between Two Points = {0} Units'.format(distance))
Python 程序使用函數尋找兩點之間的距離。
它接受兩點並返回這兩點之間的距離。
import math
def distanceBetweenTwo(x1, y1, x2, y2):
return math.sqrt((math.pow((x2 - x1), 2)) + (math.pow((y2 - y1), 2)))
x1 = int(input("Enter the First Point Coordinate x1 = "))
y1 = int(input("Enter the First Point Coordinate y1 = "))
x2 = int(input("Enter the Second Point Coordinate x2 = "))
y2 = int(input("Enter the Second Point Coordinate y2 = "))
distance = distanceBetweenTwo(x1, y1, x2, y2)
print('The Distance Between Two Points = {0} Units'.format(distance))
Enter the First Point Coordinate x1 = 1
Enter the First Point Coordinate y1 = 11
Enter the Second Point Coordinate x2 = 3
Enter the Second Point Coordinate y2 = 25
The Distance Between Two Points = 14.142135623730951 Units
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/239095.html