Python程序:计算两点间距离

创新互联python教程:

写一个 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

分享标题:Python程序:计算两点间距离
本文来源:http://www.shufengxianlan.com/qtweb/news44/433844.html

网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联