Clean is_in_triangle

This commit is contained in:
2024-06-11 02:23:26 +02:00
parent 41cab8c2ac
commit 0070fc531d
2 changed files with 29 additions and 49 deletions

View File

@@ -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

View File

@@ -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)