新聞中心
這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷(xiāo)解決方案
Python程序:計(jì)算兩點(diǎn)間距離
創(chuàng)新互聯(lián)python教程:

寫(xiě)一個(gè) Python 程序,求兩點(diǎn)之間的距離。這個(gè) python 示例接受第一個(gè)和第二個(gè)坐標(biāo)點(diǎn),并使用數(shù)學(xué)冪和 sqrt 函數(shù)計(jì)算距離。
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 程序使用函數(shù)尋找兩點(diǎn)之間的距離。
它接受兩點(diǎn)并返回這兩點(diǎn)之間的距離。
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 本文題目:Python程序:計(jì)算兩點(diǎn)間距離
本文鏈接:http://m.fisionsoft.com.cn/article/cooehjg.html


咨詢
建站咨詢
