55"""
66
77
8- def is_ear (polygon : list [tuple [float , float ]], i : int , direction : str ) -> bool :
8+ def is_ear (polygon : list [tuple [float , float ]], point_idx : int , direction : str ) -> bool :
99 """
1010 This function determines whether three points form an ear.
1111
@@ -21,20 +21,20 @@ def is_ear(polygon: list[tuple[float, float]], i: int, direction: str) -> bool:
2121 True
2222 """
2323 # Calculate indices for the previous and next vertices in the polygon.
24- prev_idx = (i - 1 ) % len (polygon )
25- next_idx = (i + 1 ) % len (polygon )
24+ prev_idx = (point_idx - 1 ) % len (polygon )
25+ next_idx = (point_idx + 1 ) % len (polygon )
2626
2727 # Retrieve the coordinates of the previous, current, and next vertices.
2828 prev_point = polygon [prev_idx ]
29- point = polygon [i ]
29+ point = polygon [point_idx ]
3030 next_point = polygon [next_idx ]
3131
3232 # Check if the vertex is convex based on the polygon's orientation.
3333 if is_convex (prev_point , point , next_point , direction ):
3434 # Check if there are any points inside the triangle formed by the current vertex
3535 # and its neighbors.
3636 for j in range (len (polygon )):
37- if j not in (prev_idx , i , next_idx ) and is_point_inside_triangle (
37+ if j not in (prev_idx , point_idx , next_idx ) and is_point_inside_triangle (
3838 prev_point , point , next_point , polygon [j ]
3939 ):
4040 return False # The 'ear' is not valid because there's a point
@@ -45,10 +45,10 @@ def is_ear(polygon: list[tuple[float, float]], i: int, direction: str) -> bool:
4545
4646
4747def is_convex (
48- p : tuple [float , float ],
48+ point : tuple [float , float ],
4949 prev_p : tuple [float , float ],
5050 next_p : tuple [float , float ],
51- direction ,
51+ direction : str ,
5252) -> bool :
5353 """
5454 Determine with the ccw, if 3 points are convex.
@@ -63,13 +63,13 @@ def is_convex(
6363 """
6464 # Calculate the cross product based on the polygon's orientation.
6565 if direction == "counter-clockwise" :
66- cross_product = (next_p [0 ] - p [0 ]) * (prev_p [1 ] - p [1 ]) - ( prev_p [ 0 ] - p [ 0 ]) * (
67- next_p [ 1 ] - p [ 1 ]
68- )
66+ cross_product = (next_p [0 ] - point [0 ]) * (prev_p [1 ] - point [1 ]) - (
67+ prev_p [ 0 ] - point [ 0 ]
68+ ) * ( next_p [ 1 ] - point [ 1 ])
6969 else :
70- cross_product = (prev_p [0 ] - p [0 ]) * (next_p [1 ] - p [1 ]) - ( next_p [ 0 ] - p [ 0 ]) * (
71- prev_p [ 1 ] - p [ 1 ]
72- )
70+ cross_product = (prev_p [0 ] - point [0 ]) * (next_p [1 ] - point [1 ]) - (
71+ next_p [ 0 ] - point [ 0 ]
72+ ) * ( prev_p [ 1 ] - point [ 1 ])
7373 # Determine if the angle is convex (cross product is non-negative).
7474 return cross_product >= 0
7575
@@ -103,7 +103,7 @@ def direction(polygon: list[tuple[float, float]]) -> str:
103103 'counter-clockwise'
104104 """
105105 # Find the point with the lowest y-coordinate (and leftmost if tied).
106- point_0 = min (polygon , key = lambda p : (p [1 ], p [0 ]))
106+ point_0 = min (polygon , key = lambda point : (point [1 ], point [0 ]))
107107 idx_p0 = polygon .index (point_0 )
108108
109109 # Calculate the indices of the previous and next points.
0 commit comments