1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
|
# Copyright (c) 2022, Manfred Moitzi
# License: MIT License
import pytest
from ezdxf.render import forms
from ezdxf.math import Vec2, is_convex_polygon_2d
def test_open_triangle():
polygon = Vec2.list([(0, 0), (1, 0), (1, 1)])
assert is_convex_polygon_2d(polygon) is True
polygon.reverse()
assert is_convex_polygon_2d(polygon) is True
def test_closed_triangle():
polygon = Vec2.list([(0, 0), (1, 0), (1, 1), (0, 0)])
assert is_convex_polygon_2d(polygon) is True
polygon.reverse()
assert is_convex_polygon_2d(polygon) is True
def test_open_square():
polygon = Vec2.list([(0, 0), (1, 0), (1, 1), (0, 1)])
assert is_convex_polygon_2d(polygon) is True
polygon.reverse()
assert is_convex_polygon_2d(polygon) is True
def test_closed_square():
polygon = Vec2.list([(0, 0), (1, 0), (1, 1), (0, 1), (0, 0)])
assert is_convex_polygon_2d(polygon) is True
polygon.reverse()
assert is_convex_polygon_2d(polygon) is True
@pytest.mark.parametrize(
"polygon",
[
Vec2.list([(0, 0)]),
Vec2.list([(0, 0), (1, 0)]),
Vec2.list([(0, 0), (1, 0), (0, 0)]),
],
)
def test_irregular_polygons(polygon):
assert is_convex_polygon_2d(polygon) is False
polygon.reverse()
assert is_convex_polygon_2d(polygon) is False
def test_two_colinear_edges():
polygon = Vec2.list([(0, 0), (1, 0), (2, 0), (0, 1)])
assert is_convex_polygon_2d(polygon) is True
polygon.reverse()
assert is_convex_polygon_2d(polygon) is True
def test_strict_mode_for_two_colinear_edges():
polygon = Vec2.list([(0, 0), (1, 0), (2, 0), (0, 1)])
assert is_convex_polygon_2d(polygon, strict=True) is False
polygon.reverse()
assert is_convex_polygon_2d(polygon, strict=True) is False
def test_all_colinear_edges():
polygon = Vec2.list([(0, 0), (1, 0), (2, 0), (3, 0)])
assert is_convex_polygon_2d(polygon) is False
polygon.reverse()
assert is_convex_polygon_2d(polygon) is False
def test_some_coincident_vertices():
polygon = Vec2.list([(0, 0), (1, 0), (1, 0), (0, 1)])
assert is_convex_polygon_2d(polygon) is True
polygon.reverse()
assert is_convex_polygon_2d(polygon) is True
def test_all_coincident_vertices():
polygon = Vec2.list([(0, 0), (0, 0), (0, 0), (0, 0)])
assert is_convex_polygon_2d(polygon) is False
polygon.reverse()
assert is_convex_polygon_2d(polygon) is False
@pytest.mark.parametrize("n", [3, 4, 11])
def test_regular_convex_ngons(n):
polygon = Vec2.list(forms.ngon(n, radius=1.0))
assert is_convex_polygon_2d(polygon) is True
polygon.reverse()
assert is_convex_polygon_2d(polygon) is True
@pytest.mark.parametrize("n", [4, 5, 11])
def test_star_is_concave(n):
polygon = Vec2.list(forms.star(n, r1=1.0, r2=2.0))
assert is_convex_polygon_2d(polygon) is False
polygon.reverse()
assert is_convex_polygon_2d(polygon) is False
def test_concave_quadrilateral():
polygon = Vec2.list([(0, 0), (1, 1), (2, 0), (1, 2)])
assert is_convex_polygon_2d(polygon) is False
polygon.reverse()
assert is_convex_polygon_2d(polygon) is False
if __name__ == "__main__":
pytest.main([__file__])
|