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

@@ -264,11 +264,3 @@ block_list = ["blue_concrete", "red_concrete", "green_concrete",
# # polyline._alpha_assign(1, polyline.length_polyline-1)
# print(polyline.alpha_radii)
s = Segment2D(Point2D(0, 0), Point2D(10, 15), 1)
print(s)
c = Circle(Point2D(0, 0), 5, 10)
print(c.circle_points(10, 10))

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":

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)

View File

@@ -17,10 +17,6 @@ def optimized_path(points, start=None):
return path
def nearest(points, start):
return min(points, key=lambda x: distance(start, x))
def sort_by_clockwise(points):
"""
Sort point in a rotation order. Works in 2d but supports 3d.