Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
505 views
in Technique[技术] by (71.8m points)

python的三维空间点求距离问题,初学者百度不到答案 现在手足无措了...

"""
声明一个类Point。描述三维空间的点
属性:x、y、z
方法:distance() 描述当前的点到另一个点之间的直线距离
定义两个Point的对象,并计算距离
((x1-x2)2 +(y1-y2)2 + (z1-z2)2) 0.5

class Point:
     x = 0
     y = 0
     z = 0
     def distance(self, x, y, z):
            sum = ((x1 - x2) ** 2 + (y1 - y2) ** 2 + (z1 - z2) ** 2) ** 0.5
     print()
    point1 = Point()
    point1.x = 3
    point1.y = 4
    point1.z = 5
    point2 = Point()
    point2.x = 5
    point2.y = 12
    point2.z = 13

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

题目应该考察的是类的使用定义啥的

class Point:
    def __init__(self, x, y, z):
        self.x = x
        self.y = y
        self.z = z
def distance(p1, p2):
    return ((p1.x - p2.x) ** 2 + (p1.y - p2.y) ** 2 + (p1.z - p2.z) ** 2) ** 0.5
p1, p2 = Point(3, 4, 5), Point(5, 12, 13)
print distance(p1, p2)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...