當前位置:成語大全網 - 新華字典 - python中怎樣定義壹個函數來計算兩點距離?

python中怎樣定義壹個函數來計算兩點距離?

import?math

class?Dot:

def?__init__(self,x,y,z):

self.x=float(x)

self.y=float(y)

self.z=float(z)

t1=input('請輸入點t1的坐標:')

t2=input('請輸入點t2的坐標:')

t1=eval('[%s]'%t1)

t2=eval('[%s]'%t2)

T1=Dot(t1[0],t1[1],t1[2])

T2=Dot(t2[0],t2[1],t2[2])

print('點t1:',T1.x,T1.y,T1.z)

print('點t2:',T2.x,T2.y,T2.z)

s=math.sqrt((T1.x-T2.x)*(T1.x-T2.x)-(T1.y-T2.y)*(T1.y-T2.y)+(T1.z-T2.z)*(T1.z-T2.z))

print("兩點間的距離為:%s"%?s)