Skip to content

Commit

Permalink
🐛 fix is_point_in_poligons and calculate_points for empty images (pyt…
Browse files Browse the repository at this point in the history
  • Loading branch information
eruvanos committed Jun 12, 2020
1 parent 820ad54 commit f67c09d
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 1 deletion.
5 changes: 4 additions & 1 deletion arcade/drawing_support.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ def sample_func(sample_point):

if sample_func(p1) and sample_func(p2) and sample_func(p3) and sample_func(p4):
# Do a quick check if it is a full tile
p1 =(-image.width / 2, -image.height / 2)
p1 = (-image.width / 2, -image.height / 2)
p2 = (image.width / 2, -image.height / 2)
p3 = (image.width / 2, image.height / 2)
p4 = (-image.width / 2, image.height / 2)
Expand Down Expand Up @@ -186,6 +186,9 @@ def segment_func(v0, v1):
sample_func)

# Select which line set to use
if len(line_set) == 0:
return []

selected_line_set = line_set[0]
selected_range = None
if len(line_set) > 1:
Expand Down
2 changes: 2 additions & 0 deletions arcade/geometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ def is_point_in_polygon(x, y, polygon_point_list):
"""
n = len(polygon_point_list)
inside = False
if n == 0:
return False

p1x, p1y = polygon_point_list[0]
for i in range(n+1):
Expand Down
17 changes: 17 additions & 0 deletions tests/unit2/test_calculate_points.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,27 @@
from uuid import uuid4

import PIL
from PIL.Image import Image

import arcade


def test_calculate_points():
texture = arcade.load_texture(":resources:images/items/coinGold.png")
result = arcade.calculate_points(texture.image)
print(result)
assert result == [(-32, 7), (-17, 28), (7, 32), (29, 15), (32, -7), (17, -28), (-8, -32), (-28, -17)]

texture = arcade.load_texture(":resources:images/animated_characters/female_person/femalePerson_idle.png")
result = arcade.calculate_points(texture.image)
print(result)
assert result == [(-32, -31), (-22, -18), (-27, -14), (-21, 21), (-8, 31), (10, 31), (25, 17), (29, -16), (22, -18),
(32, -31), (32, -52), (29, -59), (17, -57), (22, -39), (15, -28), (19, -63), (4, -64), (-2, -46),
(-3, -63), (-18, -64), (-15, -30), (-22, -39), (-16, -47), (-18, -59), (-31, -56)]


def test_empty_image():
texture = arcade.Texture(name=str(uuid4()), image=PIL.Image.new("RGBA", (50, 50)))
result = arcade.calculate_points(texture.image)
print(result)
assert result == []
18 changes: 18 additions & 0 deletions tests/unit2/test_is_point_in_poligons.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import arcade


def test_point_in_rectangle():
poligon = [
(0, 0),
(0, 50),
(50, 50),
(50, 0),
]
result = arcade.is_point_in_polygon(25, 25, poligon)
assert result is True


def test_point_not_in_empty_poligon():
poligon = []
result = arcade.is_point_in_polygon(25, 25, poligon)
assert result is False

0 comments on commit f67c09d

Please sign in to comment.