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,5 +1,6 @@
import numpy as np
import math
from typing import List
from math import atan2, sqrt
class Point2D:
@@ -47,9 +48,20 @@ class Point2D:
else:
return (s_p <= 0) and (t_p <= 0) and (s_p + t_p) >= d
def distance(self, point: "Point2D"):
def distance(self, point: "Point2D") -> int:
return sqrt((point.x - self.x) ** 2 + (point.y - self.y) ** 2)
def nearest(self, points: List["Point2D"]) -> "Point2D":
"""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:
Point2D: The nearest point, and if multiple, the first in the list.
"""
return min(points, key=lambda point: self.distance(point))
def angle(self, xy1, xy2):
"""
Compute angle (in degrees). Corner in current point.
@@ -73,7 +85,7 @@ class Point2D:
v0 = np.array(xy1.coordinate) - np.array(self.coordinate)
v1 = np.array(xy2.coordinate) - np.array(self.coordinate)
angle = math.atan2(np.linalg.det([v0, v1]), np.dot(v0, v1))
angle = atan2(np.linalg.det([v0, v1]), np.dot(v0, v1))
return np.degrees(angle)
def round(self, ndigits: int = None) -> "Point2D":