简体   繁体   English

如何使用python中的返回方法计算两点之间的距离?

[英]How to calculate the distance between two points using return methods in python?

I'm still new to python and have been trying to get the hang of it.我对 python 还是个新手,一直在努力掌握它。 I've been trying to learn simple return methods but I can't seem to get the hang of it.我一直在尝试学习简单的返回方法,但我似乎无法掌握它。 I have been trying to find the distance between two points and this is what I have so far.我一直在试图找到两点之间的距离,这就是我到目前为止所拥有的。 If anyone could help me figure this out it would be very helpful!如果有人能帮我解决这个问题,那将非常有帮助! Thank you!谢谢!

import math

def calculateDistance(x1,y1,x2,y2):
     dist = math.sqrt((x2 - x1)**2 + (y2 - y1)**2)
     return dist

calculateDistance(2,4,6,8)

print calculateDistance

Why don't you use math.hypot() to calculate the distance?为什么不使用 math.hypot() 来计算距离?

>>> import math
>>> p1 = (3, 5)  # point 1 coordinate
>>> p2 = (5, 7)  # point 2 coordinate
>>> math.hypot(p2[0] - p1[0], p2[1] - p1[1]) # Linear distance 
2.8284271247461903

Store the result in a variable将结果存储在变量中

import math

def calculateDistance(x1,y1,x2,y2):
     dist = math.sqrt((x2 - x1)**2 + (y2 - y1)**2)
     return dist

distance = calculateDistance(2,4,6,8)

print distance

Or print the result directly或者直接打印结果

import math

def calculateDistance(x1,y1,x2,y2):
     dist = math.sqrt((x2 - x1)**2 + (y2 - y1)**2)
     return dist

print calculateDistance(2,4,6,8)

You have the right idea mostly (your function logic is correct) but the syntax for using the results of the function is not correct.您的想法大多是正确的(您的函数逻辑是正确的),但是使用函数结果的语法不正确。 To get the desired results you can do one of the following:要获得所需的结果,您可以执行以下操作之一:

Save the results of the function call in the variable:将函数调用的结果保存在变量中:

def calculateDistance(x1,y1,x2,y2):
     dist = math.sqrt((x2 - x1)**2 + (y2 - y1)**2)
     return dist

some_variable = calculateDistance(2,4,6,8)

print some_variable

or print directly:或直接打印:

def calculateDistance(x1,y1,x2,y2):
     dist = math.sqrt((x2 - x1)**2 + (y2 - y1)**2)
     return dist

print calculateDistance(2,4,6,8)
print calculateDistance(2,4,6,8)

Think of it like a function in math.把它想象成数学中的一个函数。 Put the function call in the position where you need its value.将函数调用放在您需要其值的位置。 Alternatively, you can store the value in a variable:或者,您可以将值存储在变量中:

dist = calculateDistance(2,4,6,8)
print dist

(This does not work like math.) (这不像数学那样工作。)

I don't know what a "return method" is - what you have here is a simple function.我不知道什么是“返回方法”——你这里有一个简单的函数。

What you're doing, though, is calling it, but not doing anything with the result: then printing the actual function itself, rather than the result.但是,您正在做的是调用它,但不对结果做任何事情:然后打印实际的函数本身,而不是结果。

You probably mean:你可能的意思是:

distance = calculateDistance(2,4,6,8)
print distance

or even甚至

print calculateDistance(2,4,6,8)

So I'm not sure what your error is.所以我不确定你的错误是什么。

If you want the number just :如果您只想要数字:

def calcdist(x, y, x1, y1):
     return math.sqrt((x-x1)**2 + (y2-y1)**2)

dist = calcdist(#, #, #, #)
print dist

Right now you are returning the function math.sqrt(...) so when you call calculate distance with 2, 4, 6, 8 you are returning an object that has the function and the 4 parameters, I think.现在你正在返回函数math.sqrt(...)所以当你用 2, 4, 6, 8 调用计算距离时,你正在返回一个具有函数和 4 个参数的对象,我想。

Good Luck祝你好运

Imagine Python as running into the function call ( calculateDistance(2, 4, 6, 8) ), evaluating the function, and literally just replacing the line of code calculateDistance(2, 4, 6, 8) with the number that the function returns, let's say 7 .想象一下 Python 运行到函数调用( calculateDistance(2, 4, 6, 8) ),评估该函数,并且实际上只是将代码行calculateDistance(2, 4, 6, 8)替换为函数返回的数字,让我们说7

So typing calculateDistance(2, 4, 6, 8) on a line by itself will do just as much as typing 7 on a line by itself.因此calculateDistance(2, 4, 6, 8)在一行中单独键入calculateDistance(2, 4, 6, 8)与在一行中单独键入7作用相同。 You need to do something with that value, like store it in a variable您需要对该值执行某些操作,例如将其存储在变量中

dist = calculateDistance(2, 4, 6, 8)

or just print it immediately或立即打印

print calculateDistance(2, 4, 6, 8)

In Eclipse PyDev you can do it like here:在 Eclipse PyDev 中,您可以这样做:

import math
p1 = (2, 4)  
p2 = (6, 8)  
dist = math.hypot(p2[0] - p1[0], p2[1] - p1[1])

print (dist)
import math    
x1 = int(input("Enter x co-ordinate of 1st point:"))
y1 = int(input("Enter y co-ordinate of 1st point:"))

x2 = int(input("Enter x co-ordinate of 2nd point:"))
y2 = int(input("Enter y co-ordinate of 2nd point:"))

def distance_calc(a1,b1,a2,b2):
    
    d = math.sqrt((a2-a1)**2 + (b2-b1)**2)
    print(f"\nDistance: {d}")
    
distance_calc(x1,y1,x2,y2)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用欧几里得距离计算两点之间的距离? - How to calculate the distance between two points using Euclidean distance? 如何计算 python 中线上两点之间的距离 - How to calculate the distance between two points on lines in python 计算两点之间的距离作为圆的半径,然后使用 python class 计算面积和周长 - calculate distance between two points as radius of a circle then calculate area and perimeter using python class 使用距离矩阵在Python中计算经纬度点之间的距离 - Using distance matrix to calculate distance between points with latitude and longitude in Python 如何计算python中组内两点之间的距离,对于许多后续点 - How to calculate the distance between two points, for many consequent points, within groups in python 根据条件计算python中两点之间最近的邻居的距离 - Calculate distance of nearest neighbor in python between two points based on criteria 替代(python)计算两个不同集合中所有点之间的距离 - Alternative (python) to calculate distance between all points at two different sets 计算 python 中两个列表的每个 GPS 点之间的距离 - Calculate the distance between each GPS points of two lists in python 如何使用 python opencv 计算两个人之间的距离? - How to calculate distance between two person using python opencv? 我试图使用类计算两点之间的笛卡尔距离 - Im trying to calculate cartesian distance between two points using classes
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM