From 5ea926c9f859a1fcdd3c74c84373791483f042a7 Mon Sep 17 00:00:00 2001 From: Xeon0X Date: Tue, 11 Jun 2024 02:26:18 +0200 Subject: [PATCH] Clean distance --- networks/geometry/Point2D.py | 6 +++++- networks/geometry/Point3D.py | 3 +++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/networks/geometry/Point2D.py b/networks/geometry/Point2D.py index 54a1366..c50a6a7 100644 --- a/networks/geometry/Point2D.py +++ b/networks/geometry/Point2D.py @@ -18,6 +18,8 @@ class Point2D: def is_in_triangle(self, xy0: Type[Point2D], xy1: Type[Point2D], xy2: Type[Point2D]): """Returns True is the point is in a triangle defined by 3 others points. + From: https://stackoverflow.com/questions/2049582/how-to-determine-if-a-point-is-in-a-2d-triangle#:~:text=A%20simple%20way%20is%20to,point%20is%20inside%20the%20triangle. + Args: xy0 (Type[Point2D]): Point of the triangle. xy1 (Type[Point2D]): Point of the triangle. @@ -26,7 +28,6 @@ class Point2D: Returns: bool: False if the point is not inside the triangle. """ - # https://stackoverflow.com/questions/2049582/how-to-determine-if-a-point-is-in-a-2d-triangle#:~:text=A%20simple%20way%20is%20to,point%20is%20inside%20the%20triangle. dx = self.x - xy0.x dy = self.y - xy0.y @@ -43,3 +44,6 @@ class Point2D: return (s_p >= 0) and (t_p >= 0) and (s_p + t_p) <= d else: return (s_p <= 0) and (t_p <= 0) and (s_p + t_p) >= d + + def distance(self, point: Type[Point2D]): + return sqrt((point.x - self.x) ** 2 + (point.y - self.y) ** 2) diff --git a/networks/geometry/Point3D.py b/networks/geometry/Point3D.py index c85b250..393fa24 100644 --- a/networks/geometry/Point3D.py +++ b/networks/geometry/Point3D.py @@ -15,3 +15,6 @@ class Point3D: def __repr__(self): return f"Point2D(x: {self.x}, y: {self.y}, z: {self.z})" + + def distance(self, point: Type[Point3D]): + return sqrt((point.x - self.x) ** 2 + (point.y - self.y) ** 2 + (point.z - self.z) ** 2)