From 0070fc531d46a09a29361b6c083c577b393ed54d Mon Sep 17 00:00:00 2001 From: Xeon0X Date: Tue, 11 Jun 2024 02:23:26 +0200 Subject: [PATCH] Clean is_in_triangle --- networks/geometry/Point2D.py | 29 +++++++++++++++++++ networks/geometry/point_tools.py | 49 -------------------------------- 2 files changed, 29 insertions(+), 49 deletions(-) diff --git a/networks/geometry/Point2D.py b/networks/geometry/Point2D.py index 435172a..54a1366 100644 --- a/networks/geometry/Point2D.py +++ b/networks/geometry/Point2D.py @@ -14,3 +14,32 @@ class Point2D: def __repr__(self): return f"Point2D(x: {self.x}, y: {self.y})" + + 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. + + Args: + xy0 (Type[Point2D]): Point of the triangle. + xy1 (Type[Point2D]): Point of the triangle. + xy2 (Type[Point2D]): Point of the triangle. + + 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 + + dx2 = xy2.x - xy0.x + dy2 = xy2.y - xy0.y + dx1 = xy1.x - xy0.x + dy1 = xy1.y - xy0.y + + s_p = (dy2 * dx) - (dx2 * dy) + t_p = (dx1 * dy) - (dy1 * dx) + d = (dx1 * dy2) - (dy1 * dx2) + + if d > 0: + 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 diff --git a/networks/geometry/point_tools.py b/networks/geometry/point_tools.py index ae85eba..24ab3db 100644 --- a/networks/geometry/point_tools.py +++ b/networks/geometry/point_tools.py @@ -3,55 +3,6 @@ import numpy as np from networks.geometry.segment_tools import discrete_segment, middle_point, parallel -def circle(center, radius): - """ - Can be used for circle or disc. Works in 2d but supports 3d. - - Args: - xyC (tuple): Coordinates of the center. - r (int): Radius of the circle. - - Returns: - dict: Keys are distance from the circle. Value is a list of all - coordinates at this distance. 0 for a circle. Negative values - for a disc, positive values for a hole. - """ - area = ( - (round(center[0]) - round(radius), round(center[-1]) - round(radius)), - (round(center[0]) + round(radius) + 1, - round(center[-1]) + round(radius) + 1), - ) - - circle = {} - for x in range(area[0][0], area[1][0]): - for y in range(area[0][1], area[1][1]): - d = round(distance((x, y), (center))) - radius - if circle.get(d) == None: - circle[d] = [] - circle[d].append((x, y)) - return circle - - -def is_in_triangle(point, xy0, xy1, xy2): - # Works in 2d but supports 3d. - # 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 = point[0] - xy0[0] - dY = point[-1] - xy0[-1] - dX20 = xy2[0] - xy0[0] - dY20 = xy2[-1] - xy0[-1] - dX10 = xy1[0] - xy0[0] - dY10 = xy1[-1] - xy0[-1] - - s_p = (dY20 * dX) - (dX20 * dY) - t_p = (dX10 * dY) - (dY10 * dX) - D = (dX10 * dY20) - (dY10 * dX20) - - if D > 0: - 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(xy1, xy2): # TODO : Can be better. # Works in 2d but supports 3d. return sqrt((xy2[0] - xy1[0]) ** 2 + (xy2[-1] - xy1[-1]) ** 2)