Add nearest support to Point3D

This commit is contained in:
2024-06-11 17:04:40 +02:00
parent b39c9d13dd
commit ac86f8588e
4 changed files with 34 additions and 16 deletions

View File

@@ -1,3 +1,7 @@
from typing import List
from math import atan2, sqrt
class Point3D:
def __init__(self, x: int, y: int, z: int):
self.x = x
@@ -11,11 +15,25 @@ class Point3D:
return (self.x, self.y, self.z)
def __repr__(self):
return f"Point2D(x: {self.x}, y: {self.y}, z: {self.z})"
return f"Point3D(x: {self.x}, y: {self.y}, z: {self.z})"
def distance(self, point: "Point3D"):
return sqrt((point.x - self.x) ** 2 + (point.y - self.y) ** 2 + (point.z - self.z) ** 2)
def nearest(self, points: List["Point3D"]) -> "Point3D":
"""Return the nearest point. If multiple nearest point, returns the first in the list.
Args:
points (List[Point2D]): List of the points to test.
Returns:
Point3D: The nearest point, and if multiple, the first in the list.
>>> print(Point3D(0, 0, 0).nearest((Point3D(-10, 10, 5), Point3D(10, 10, 1))))
Point3D(x: 10, y: 10, z: 1)
"""
return min(points, key=lambda point: self.distance(point))
def round(self, ndigits: int = None) -> "Point3D":
self.x = round(self.x, ndigits)
self.y = round(self.y, ndigits)