Skip to content

Point in polygon fix #2336

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions arcade/geometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,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]'
Expand Down
16 changes: 10 additions & 6 deletions tests/unit/sprite/test_sprite_collision.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure why these comments are here

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just remove if they don't make sense. It means they have low value.

# 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
Expand All @@ -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
Expand Down