mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-11-24 05:21:09 +00:00
Pythagoras (#1243)
* add pythagoras.py * function distance * run as script * Update pythagoras.py
This commit is contained in:
parent
415c9f5e65
commit
4dca9571db
33
maths/pythagoras.py
Normal file
33
maths/pythagoras.py
Normal file
|
@ -0,0 +1,33 @@
|
||||||
|
"""Uses Pythagoras theorem to calculate the distance between two points in space."""
|
||||||
|
|
||||||
|
import math
|
||||||
|
|
||||||
|
|
||||||
|
class Point:
|
||||||
|
def __init__(self, x, y, z):
|
||||||
|
self.x = x
|
||||||
|
self.y = y
|
||||||
|
self.z = z
|
||||||
|
|
||||||
|
def __repr__(self) -> str:
|
||||||
|
return f"Point({self.x}, {self.y}, {self.z})"
|
||||||
|
|
||||||
|
|
||||||
|
def distance(a: Point, b: Point) -> float:
|
||||||
|
return math.sqrt(abs(((b.x - a.x) ** 2 + (b.y - a.y) ** 2 + (b.z - a.z) ** 2)))
|
||||||
|
|
||||||
|
|
||||||
|
def test_distance() -> None:
|
||||||
|
"""
|
||||||
|
>>> 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
|
||||||
|
"""
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
import doctest
|
||||||
|
|
||||||
|
doctest.testmod()
|
Loading…
Reference in New Issue
Block a user