81 lines
2.8 KiB
Python
81 lines
2.8 KiB
Python
from numpy.testing import assert_equal
|
|
|
|
from ..shapes import Path
|
|
|
|
|
|
def test_path_init() -> None:
|
|
p = Path(vertices=[[0, 0], [10, 0]], width=2, cap=Path.Cap.Flush)
|
|
assert_equal(p.vertices, [[0, 0], [10, 0]])
|
|
assert p.width == 2
|
|
assert p.cap == Path.Cap.Flush
|
|
|
|
|
|
def test_path_to_polygons_flush() -> None:
|
|
p = Path(vertices=[[0, 0], [10, 0]], width=2, cap=Path.Cap.Flush)
|
|
polys = p.to_polygons()
|
|
assert len(polys) == 1
|
|
# Rectangle from (0, -1) to (10, 1)
|
|
bounds = polys[0].get_bounds_single()
|
|
assert_equal(bounds, [[0, -1], [10, 1]])
|
|
|
|
|
|
def test_path_to_polygons_square() -> None:
|
|
p = Path(vertices=[[0, 0], [10, 0]], width=2, cap=Path.Cap.Square)
|
|
polys = p.to_polygons()
|
|
assert len(polys) == 1
|
|
# Square cap adds width/2 = 1 to each end
|
|
# Rectangle from (-1, -1) to (11, 1)
|
|
bounds = polys[0].get_bounds_single()
|
|
assert_equal(bounds, [[-1, -1], [11, 1]])
|
|
|
|
|
|
def test_path_to_polygons_circle() -> None:
|
|
p = Path(vertices=[[0, 0], [10, 0]], width=2, cap=Path.Cap.Circle)
|
|
polys = p.to_polygons(num_vertices=32)
|
|
# Path.to_polygons for Circle cap returns 1 polygon for the path + polygons for the caps
|
|
assert len(polys) >= 3
|
|
|
|
# Combined bounds should be from (-1, -1) to (11, 1)
|
|
# But wait, Path.get_bounds_single() handles this more directly
|
|
bounds = p.get_bounds_single()
|
|
assert_equal(bounds, [[-1, -1], [11, 1]])
|
|
|
|
|
|
def test_path_custom_cap() -> None:
|
|
p = Path(vertices=[[0, 0], [10, 0]], width=2, cap=Path.Cap.SquareCustom, cap_extensions=(5, 10))
|
|
polys = p.to_polygons()
|
|
assert len(polys) == 1
|
|
# Extends 5 units at start, 10 at end
|
|
# Starts at -5, ends at 20
|
|
bounds = polys[0].get_bounds_single()
|
|
assert_equal(bounds, [[-5, -1], [20, 1]])
|
|
|
|
|
|
def test_path_bend() -> None:
|
|
# L-shaped path
|
|
p = Path(vertices=[[0, 0], [10, 0], [10, 10]], width=2)
|
|
polys = p.to_polygons()
|
|
assert len(polys) == 1
|
|
bounds = polys[0].get_bounds_single()
|
|
# Outer corner at (11, -1) is not right.
|
|
# Segments: (0,0)-(10,0) and (10,0)-(10,10)
|
|
# Corners of segment 1: (0,1), (10,1), (10,-1), (0,-1)
|
|
# Corners of segment 2: (9,0), (9,10), (11,10), (11,0)
|
|
# Bounds should be [[-1 (if start is square), -1], [11, 11]]?
|
|
# Flush cap start at (0,0) with width 2 means y from -1 to 1.
|
|
# Vertical segment end at (10,10) with width 2 means x from 9 to 11.
|
|
# So bounds should be x: [0, 11], y: [-1, 10]
|
|
assert_equal(bounds, [[0, -1], [11, 10]])
|
|
|
|
|
|
def test_path_mirror() -> None:
|
|
p = Path(vertices=[[10, 5], [20, 10]], width=2)
|
|
p.mirror(0) # Mirror across x axis (y -> -y)
|
|
assert_equal(p.vertices, [[10, -5], [20, -10]])
|
|
|
|
|
|
def test_path_scale() -> None:
|
|
p = Path(vertices=[[0, 0], [10, 0]], width=2)
|
|
p.scale_by(2)
|
|
assert_equal(p.vertices, [[0, 0], [20, 0]])
|
|
assert p.width == 4
|