[Python]計(jì)算兩點(diǎn)之間的距離

1. 說(shuō)明:
  1. 共有兩個(gè).py文件,分別為Point.py和TestPoint.py
  2. 兩個(gè)文件位于相同的文件夾下
2. 文件Point.py :
import math

class Point:
    def reset(self):
        self.x=0
        self.y=0

    def move(self,x,y):
        self.x = x
        self.y = y

    def calculate_distance(self,otherPoint):
        d_x = otherPoint.x - self.x
        d_y = otherPoint.y - self.y
        #計(jì)算兩點(diǎn)之間的距離
        distance = math.sqrt(d_x**2 + d_y**2)
        return distance
3. 文件TestPoint.py :
#調(diào)用Point.py文件中類Point
#from Point import Point
# p1 = Point()
# p2 = Point()

#導(dǎo)入Point類
import Point

p1 = Point.Point()
p2 = Point.Point()

p1.reset()
p2.move(6,8)

distan = p1.calculate_distance(p2)
print('兩點(diǎn)之間的距離為',distan)

輸出結(jié)果:

D:\python_project\venv\Scripts\python.exe D:/python_project/TestPoint.py
兩點(diǎn)之間的距離為 10.0

Process finished with exit code 0
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容