diff --git a/src/data_morph/bounds/bounding_box.py b/src/data_morph/bounds/bounding_box.py index 8aa4d8c2..38804601 100644 --- a/src/data_morph/bounds/bounding_box.py +++ b/src/data_morph/bounds/bounding_box.py @@ -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: diff --git a/src/data_morph/shapes/lines/star.py b/src/data_morph/shapes/lines/star.py index bb23ce35..b5b339d1 100644 --- a/src/data_morph/shapes/lines/star.py +++ b/src/data_morph/shapes/lines/star.py @@ -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], diff --git a/tests/bounds/test_bounding_box.py b/tests/bounds/test_bounding_box.py index 83633262..130fb323 100644 --- a/tests/bounds/test_bounding_box.py +++ b/tests/bounds/test_bounding_box.py @@ -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.""" diff --git a/tests/shapes/lines/test_star.py b/tests/shapes/lines/test_star.py index dcafb03f..fbe9fb64 100644 --- a/tests/shapes/lines/test_star.py +++ b/tests/shapes/lines/test_star.py @@ -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