forked from TheAlgorithms/Python
- Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpythagoras.py
29 lines (20 loc) · 712 Bytes
/
pythagoras.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
"""Uses Pythagoras theorem to calculate the distance between two points in space."""
importmath
classPoint:
def__init__(self, x, y, z):
self.x=x
self.y=y
self.z=z
def__repr__(self) ->str:
returnf"Point({self.x}, {self.y}, {self.z})"
defdistance(a: Point, b: Point) ->float:
"""
>>> point1 = Point(2, -1, 7)
>>> point2 = Point(1, -3, 5)
>>> print(f"Distance from {point1} to {point2} is {distance(point1, point2)}")
Distance from Point(2, -1, 7) to Point(1, -3, 5) is 3.0
"""
returnmath.sqrt(abs((b.x-a.x) **2+ (b.y-a.y) **2+ (b.z-a.z) **2))
if__name__=="__main__":
importdoctest
doctest.testmod()