Skip to content
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
22 changes: 18 additions & 4 deletions src/data_morph/bounds/bounding_box.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,14 +133,28 @@ def adjust_bounds(self, x: Number | None = None, y: Number | None = None) -> Non
if y:
self.y_bounds.adjust_bounds(y)

def align_aspect_ratio(self) -> None:
"""Align the aspect ratio to 1:1."""
def align_aspect_ratio(self, shrink: bool = False) -> None:
"""
Align the aspect ratio to 1:1.

Parameters
----------
shrink : bool, default ``False``
Whether to shrink the larger bound (``True``), or
expand the smaller bound (``False``).
"""
x_range, y_range = self.range
diff = x_range - y_range
if diff < 0:
self.adjust_bounds(x=-diff)
if shrink:
self.adjust_bounds(y=diff)
else:
self.adjust_bounds(x=-diff)
elif diff > 0:
self.adjust_bounds(y=diff)
if shrink:
self.adjust_bounds(x=-diff)
else:
self.adjust_bounds(y=diff)

@property
def aspect_ratio(self) -> Number:
Expand Down
14 changes: 4 additions & 10 deletions src/data_morph/shapes/lines/star.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,11 @@ class Star(LineCollection):
"""

def __init__(self, dataset: Dataset) -> None:
bounds = dataset.data_bounds.clone()
bounds.align_aspect_ratio()
bounds = dataset.morph_bounds.clone()
bounds.align_aspect_ratio(shrink=True)

x_bounds = bounds.x_bounds
y_bounds = bounds.y_bounds

xmin, xmax = x_bounds
ymin, ymax = y_bounds

x_range = x_bounds.range
y_range = y_bounds.range
(xmin, xmax), (ymin, ymax) = bounds
x_range, y_range = bounds.range

pts = [
[xmin, ymin + y_range * 0.625],
Expand Down
13 changes: 7 additions & 6 deletions tests/bounds/test_bounding_box.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,18 +165,19 @@ def test_adjust_bounds(self, x, y):
assert new_range_y == initial_range_y + y

@pytest.mark.parametrize(
('x', 'y'),
('x', 'y', 'shrink', 'expected_range'),
[
([10, 90], [500, 600]),
([500, 600], [10, 90]),
([10, 90], [10, 90]),
([10, 90], [500, 600], False, 100),
([500, 600], [10, 90], True, 80),
([10, 90], [10, 90], False, 80),
],
)
def test_align_aspect_ratio(self, x, y):
def test_align_aspect_ratio(self, x, y, shrink, expected_range):
"""Test that the align_aspect_ratio() method is working."""
bbox = BoundingBox(x, y)
bbox.align_aspect_ratio()
bbox.align_aspect_ratio(shrink)
assert pytest.approx(bbox.aspect_ratio) == 1
assert bbox.x_bounds.range == bbox.y_bounds.range == expected_range

def test_clone(self):
"""Test that the clone() method is working."""
Expand Down
15 changes: 14 additions & 1 deletion tests/shapes/lines/test_star.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,18 @@ class TestStar(PolygonsLineModuleTestBase):
"""Test the Star class."""

shape_name = 'star'
distance_test_cases = (((20, 50), 5.856516), ((30, 60), 3.709127))
distance_test_cases = (
((8, 68), 0),
((17, 68), 0),
((20, 77), 0),
((23, 68), 0),
((32, 68), 0),
((24.5, 62), 0),
((27.5, 53), 0),
((20, 59), 0),
((12.5, 53), 0),
((15.5, 62), 0),
((20, 50), 7.027819284987274),
((30, 60), 4.58530260724415),
)
expected_line_count = 10