Skip to content

Using infinity in is_point_in_polygon + micro cleaning #1957

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 1 commit into from
Feb 10, 2024
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
6 changes: 6 additions & 0 deletions CONTRIBUTING.rst
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,14 @@ terminal from inside the top level of the arcade directory:

.. code-block:: shell

# For Unix-like shells (Linux, macOS)
pip install -e '.[dev]'

.. code-block:: shell

# For Windows
pip install -e .[dev]

If you get an error like the one below, you probably need to update your pip version:

.. code-block:: text
Expand Down
14 changes: 8 additions & 6 deletions arcade/geometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from __future__ import annotations

from arcade.types import Point, PointList
from sys import maxsize as sys_int_maxsize


def are_polygons_intersecting(poly_a: PointList, poly_b: PointList) -> bool:
Expand All @@ -18,18 +19,19 @@ def are_polygons_intersecting(poly_a: PointList, poly_b: PointList) -> bool:
:param poly_b: List of points that define the second polygon.
:Returns: True or false depending if polygons intersect
"""
#if either are [], they don't intersect
# if either are [], they don't intersect
if not poly_a or not poly_b:
return False
for polygon in (poly_a, poly_b):

for i1 in range(len(polygon)):
i2 = (i1 + 1) % len(polygon)
projection_1 = polygon[i1]
projection_2 = polygon[i2]

normal = (projection_2[1] - projection_1[1],
projection_1[0] - projection_2[0])
normal = (
projection_2[1] - projection_1[1],
projection_1[0] - projection_2[0],
)

min_a, min_b = (float("inf"),) * 2
max_a, max_b = (-float("inf"),) * 2
Expand All @@ -56,6 +58,7 @@ def are_polygons_intersecting(poly_a: PointList, poly_b: PointList) -> bool:

return True


def is_point_in_box(p: Point, q: Point, r: Point) -> bool:
"""
Return True if point q is inside the box defined by p and r.
Expand Down Expand Up @@ -157,7 +160,7 @@ def is_point_in_polygon(x: float, y: float, polygon: PointList) -> bool:

# Create a point for line segment
# from p to infinite
extreme = (10000, p[1])
extreme = (sys_int_maxsize, p[1])

# To count number of points in polygon
# whose y-coordinate is equal to
Expand Down Expand Up @@ -198,4 +201,3 @@ def is_point_in_polygon(x: float, y: float, polygon: PointList) -> bool:

# Return true if count is odd, false otherwise
return count % 2 == 1

1 change: 0 additions & 1 deletion benchmarks/collisions/bench.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import arcade
import pyglet
import random
import time

SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
Expand Down
16 changes: 9 additions & 7 deletions tests/unit/test_geometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,11 @@ def test_point_not_in_empty_polygon():
assert result is False


def test_are_polygons_intersecting():
poly_a = [(0, 0), (0, 50), (50, 50), (50, 0)]
poly_b = [(25, 25), (25, 75), (75, 75), (75, 25)]
assert are_polygons_intersecting(poly_a, poly_b) is True
def test_point_in_extreme_polygon():
# Cf : https://github.com/pythonarcade/arcade/issues/1906
polygon = [(9984.0, 2112.0), (10048.0, 2112.0), (10048.0, 2048.0), (9984.0, 2048.0)]

assert is_point_in_polygon(10016.0, 2080.0, polygon)


def test_are_polygons_intersecting():
Expand All @@ -48,12 +49,13 @@ def test_are_polygons_intersecting():


def test_are_empty_polygons_breaking():
poly_a = []
poly_a = []
poly_b = []
assert are_polygons_intersecting(poly_a, poly_b) is False


def test_are_mismatched_polygons_breaking():
poly_a = [(0, 0), (0, 50), (50, 50), (50, 0)]
poly_a = [(0, 0), (0, 50), (50, 50), (50, 0)]
poly_b = []
assert are_polygons_intersecting(poly_a, poly_b) is False

Expand All @@ -74,7 +76,7 @@ def test_are_lines_intersecting():
line_b = [(0, 0), (50, 50)]
assert are_lines_intersecting(*line_a, *line_b) is True

#---------
# ---------
# Two lines clearly intersecting
line_a = [(0, 0), (50, 50)]
line_b = [(0, 50), (50, 0)]
Expand Down