From 239220d9164b6f012bd833a1c8c0dadea8b3c570 Mon Sep 17 00:00:00 2001 From: fengchiw Date: Sun, 4 Aug 2024 11:12:12 -0400 Subject: [PATCH 1/2] Fixed get point in poly when getting a horizontal edge --- arcade/geometry.py | 23 ++++++++++++++-------- tests/unit/sprite/test_sprite_collision.py | 16 +++++++++------ 2 files changed, 25 insertions(+), 14 deletions(-) diff --git a/arcade/geometry.py b/arcade/geometry.py index 0f3b6deb87..70dc7c8fe3 100644 --- a/arcade/geometry.py +++ b/arcade/geometry.py @@ -155,17 +155,14 @@ def are_lines_intersecting(p1: Point2, q1: Point2, p2: Point2, q2: Point2) -> bo return False -def is_point_in_polygon(x: float, y: float, polygon: Point2List) -> bool: +def is_point_in_polygon(x: float, y: float, polygon: PointList) -> bool: """ Checks if a point is inside a polygon of three or more points. - Args: - x: X coordinate of point - y: Y coordinate of point - polygon: List of points that define the polygon. - - Returns: - bool: ``True`` or ``False`` depending if point is inside polygon + :param x: X coordinate of point + :param y: Y coordinate of point + :param polygon_point_list: List of points that define the polygon. + :Returns: True or false depending if point is inside polygon """ p = x, y n = len(polygon) @@ -191,6 +188,16 @@ def is_point_in_polygon(x: float, y: float, polygon: Point2List) -> bool: if polygon[i][1] == p[1]: decrease += 1 + # Check if the point is exactly on a horizontal edge + if polygon[i][1] == y and polygon[next_item][1] == y: + # Check if the point is on the line segment + if (polygon[i][0] <= x <= polygon[next_item][0]) or ( + polygon[next_item][0] <= x <= polygon[i][0] + ): + return True + else: + return False + # Check if the line segment from 'p' to # 'extreme' intersects with the line # segment from 'polygon[i]' to 'polygon[next]' diff --git a/tests/unit/sprite/test_sprite_collision.py b/tests/unit/sprite/test_sprite_collision.py index 942c0ebbeb..85f3e75c49 100644 --- a/tests/unit/sprite/test_sprite_collision.py +++ b/tests/unit/sprite/test_sprite_collision.py @@ -7,16 +7,22 @@ def test_sprites_at_point(): coin_list = arcade.SpriteList() sprite = arcade.SpriteSolidColor(50, 50, color=arcade.csscolor.RED) + # an adjacent sprite with the same level horizontal bottom edge + sprite2 = arcade.SpriteSolidColor(50, 50, center_x=50, center_y=0, color=arcade.csscolor.RED) + coin_list.append(sprite) + coin_list.append(sprite2) - # print() - # print(sprite.points) sprite_list = arcade.get_sprites_at_point((0, 0), coin_list) assert len(sprite_list) == 1 + sprite_list = arcade.get_sprites_at_point((0, 50), coin_list) + assert len(sprite_list) == 1 + + sprite_list = arcade.get_sprites_at_point((0, -25), coin_list) + assert len(sprite_list) == 1 + sprite.position = (130, 130) - # print() - # print(sprite.points) sprite_list = arcade.get_sprites_at_point((0, 0), coin_list) assert len(sprite_list) == 0 @@ -25,8 +31,6 @@ def test_sprites_at_point(): assert len(sprite_list) == 1 sprite.angle = 90 - # print() - # print(sprite.points) sprite_list = arcade.get_sprites_at_point((0, 0), coin_list) assert len(sprite_list) == 0 From 96599cdce2c6f79a9e0a9c27e488105a3b610f10 Mon Sep 17 00:00:00 2001 From: fengchiw Date: Sun, 4 Aug 2024 11:16:24 -0400 Subject: [PATCH 2/2] legacy changes --- arcade/geometry.py | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/arcade/geometry.py b/arcade/geometry.py index 70dc7c8fe3..50de80c82c 100644 --- a/arcade/geometry.py +++ b/arcade/geometry.py @@ -155,14 +155,17 @@ def are_lines_intersecting(p1: Point2, q1: Point2, p2: Point2, q2: Point2) -> bo return False -def is_point_in_polygon(x: float, y: float, polygon: PointList) -> bool: +def is_point_in_polygon(x: float, y: float, polygon: Point2List) -> bool: """ Checks if a point is inside a polygon of three or more points. - :param x: X coordinate of point - :param y: Y coordinate of point - :param polygon_point_list: List of points that define the polygon. - :Returns: True or false depending if point is inside polygon + Args: + x: X coordinate of point + y: Y coordinate of point + polygon: List of points that define the polygon. + + Returns: + bool: ``True`` or ``False`` depending if point is inside polygon """ p = x, y n = len(polygon)