From c1266274f60a5429d0cf4e77f0c2c2d6c91d9902 Mon Sep 17 00:00:00 2001 From: Richard Date: Mon, 11 Nov 2024 16:16:40 +0000 Subject: [PATCH] ensure grid is properly tiled --- .../triangles/abstract_coordinate_array.py | 17 ++++++----------- .../test_coordinate_implementation.py | 18 ++++++++++++++++++ 2 files changed, 24 insertions(+), 11 deletions(-) diff --git a/autoarray/structures/triangles/abstract_coordinate_array.py b/autoarray/structures/triangles/abstract_coordinate_array.py index 0b831698..72267275 100644 --- a/autoarray/structures/triangles/abstract_coordinate_array.py +++ b/autoarray/structures/triangles/abstract_coordinate_array.py @@ -140,23 +140,18 @@ def for_limits_and_scale( scale: float = 1.0, **_, ): + x_shift = int(2 * x_min / scale) + y_shift = int(y_min / (HEIGHT_FACTOR * scale)) + coordinates = [] - x = x_min - while x < x_max: - y = y_min - while y < y_max: - coordinates.append([x, y]) - y += scale - x += scale - x_mean = (x_min + x_max) / 2 - y_mean = (y_min + y_max) / 2 + for x in range(x_shift, int(2 * x_max / scale) + 1): + for y in range(y_shift - 1, int(y_max / (HEIGHT_FACTOR * scale)) + 2): + coordinates.append([x, y]) return cls( coordinates=np.array(coordinates), side_length=scale, - x_offset=x_mean, - y_offset=y_mean, ) @property diff --git a/test_autoarray/structures/triangles/test_coordinate_implementation.py b/test_autoarray/structures/triangles/test_coordinate_implementation.py index 3e495de0..96b040b2 100644 --- a/test_autoarray/structures/triangles/test_coordinate_implementation.py +++ b/test_autoarray/structures/triangles/test_coordinate_implementation.py @@ -296,3 +296,21 @@ def test_triangles_touch(): np.array([[0, 0], [0, 1]]), ) assert max(triangles.triangles[0][:, 1]) == min(triangles.triangles[1][:, 1]) + + +def test_from_grid_regression(): + triangles = CoordinateArrayTriangles.for_limits_and_scale( + x_min=-4.75, + x_max=4.75, + y_min=-4.75, + y_max=4.75, + scale=0.5, + ) + + x = triangles.vertices[:, 0] + assert min(x) <= -4.75 + assert max(x) >= 4.75 + + y = triangles.vertices[:, 1] + assert min(y) <= -4.75 + assert max(y) >= 4.75