Skip to content

Commit 19c5c37

Browse files
authored
Make it possible to shrink to align aspect ratio and update star shape (#293)
* Modify BoundingBox.adjust_aspect_ratio() to optionally shrink to smallest bounds range * Adjust star creation logic
1 parent 971ea68 commit 19c5c37

File tree

4 files changed

+43
-21
lines changed

4 files changed

+43
-21
lines changed

src/data_morph/bounds/bounding_box.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -133,14 +133,28 @@ def adjust_bounds(self, x: Number | None = None, y: Number | None = None) -> Non
133133
if y:
134134
self.y_bounds.adjust_bounds(y)
135135

136-
def align_aspect_ratio(self) -> None:
137-
"""Align the aspect ratio to 1:1."""
136+
def align_aspect_ratio(self, shrink: bool = False) -> None:
137+
"""
138+
Align the aspect ratio to 1:1.
139+
140+
Parameters
141+
----------
142+
shrink : bool, default ``False``
143+
Whether to shrink the larger bound (``True``), or
144+
expand the smaller bound (``False``).
145+
"""
138146
x_range, y_range = self.range
139147
diff = x_range - y_range
140148
if diff < 0:
141-
self.adjust_bounds(x=-diff)
149+
if shrink:
150+
self.adjust_bounds(y=diff)
151+
else:
152+
self.adjust_bounds(x=-diff)
142153
elif diff > 0:
143-
self.adjust_bounds(y=diff)
154+
if shrink:
155+
self.adjust_bounds(x=-diff)
156+
else:
157+
self.adjust_bounds(y=diff)
144158

145159
@property
146160
def aspect_ratio(self) -> Number:

src/data_morph/shapes/lines/star.py

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,11 @@ class Star(LineCollection):
2626
"""
2727

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

32-
x_bounds = bounds.x_bounds
33-
y_bounds = bounds.y_bounds
34-
35-
xmin, xmax = x_bounds
36-
ymin, ymax = y_bounds
37-
38-
x_range = x_bounds.range
39-
y_range = y_bounds.range
32+
(xmin, xmax), (ymin, ymax) = bounds
33+
x_range, y_range = bounds.range
4034

4135
pts = [
4236
[xmin, ymin + y_range * 0.625],

tests/bounds/test_bounding_box.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -165,18 +165,19 @@ def test_adjust_bounds(self, x, y):
165165
assert new_range_y == initial_range_y + y
166166

167167
@pytest.mark.parametrize(
168-
('x', 'y'),
168+
('x', 'y', 'shrink', 'expected_range'),
169169
[
170-
([10, 90], [500, 600]),
171-
([500, 600], [10, 90]),
172-
([10, 90], [10, 90]),
170+
([10, 90], [500, 600], False, 100),
171+
([500, 600], [10, 90], True, 80),
172+
([10, 90], [10, 90], False, 80),
173173
],
174174
)
175-
def test_align_aspect_ratio(self, x, y):
175+
def test_align_aspect_ratio(self, x, y, shrink, expected_range):
176176
"""Test that the align_aspect_ratio() method is working."""
177177
bbox = BoundingBox(x, y)
178-
bbox.align_aspect_ratio()
178+
bbox.align_aspect_ratio(shrink)
179179
assert pytest.approx(bbox.aspect_ratio) == 1
180+
assert bbox.x_bounds.range == bbox.y_bounds.range == expected_range
180181

181182
def test_clone(self):
182183
"""Test that the clone() method is working."""

tests/shapes/lines/test_star.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,18 @@ class TestStar(PolygonsLineModuleTestBase):
1111
"""Test the Star class."""
1212

1313
shape_name = 'star'
14-
distance_test_cases = (((20, 50), 5.856516), ((30, 60), 3.709127))
14+
distance_test_cases = (
15+
((8, 68), 0),
16+
((17, 68), 0),
17+
((20, 77), 0),
18+
((23, 68), 0),
19+
((32, 68), 0),
20+
((24.5, 62), 0),
21+
((27.5, 53), 0),
22+
((20, 59), 0),
23+
((12.5, 53), 0),
24+
((15.5, 62), 0),
25+
((20, 50), 7.027819284987274),
26+
((30, 60), 4.58530260724415),
27+
)
1528
expected_line_count = 10

0 commit comments

Comments
 (0)