From c7f2318d6b770641a69f631128f91795be2a319b Mon Sep 17 00:00:00 2001 From: prajwel Date: Sun, 27 Aug 2023 02:04:53 +0530 Subject: [PATCH 1/2] Added directional_offset_by function to PixCoord. --- regions/core/pixcoord.py | 20 ++++++++++++++++++++ regions/core/tests/test_pixcoord.py | 12 ++++++++++++ 2 files changed, 32 insertions(+) diff --git a/regions/core/pixcoord.py b/regions/core/pixcoord.py index 8d938666..49db0c17 100644 --- a/regions/core/pixcoord.py +++ b/regions/core/pixcoord.py @@ -247,3 +247,23 @@ def rotate(self, center, angle): vec = np.matmul(rotation_matrix, vec) return self.__class__(center.x + vec[0], center.y + vec[1]) + + def directional_offset_by(self, separation, angle): + """ + Computes coordinates at the given offset from this coordinate. + + Parameters + ---------- + separation : `numpy.array` + The separation in pixels. + angle : `~astropy.coordinates.Angle` + The rotation angle. + + Returns + ------- + coord : `PixCoord` + The offseted coordinates. + """ + offset_x = self.x + separation * np.cos(angle) + offset_y = self.y + separation * np.sin(angle) + return PixCoord(offset_x, offset_y) \ No newline at end of file diff --git a/regions/core/tests/test_pixcoord.py b/regions/core/tests/test_pixcoord.py index 096a6cf4..b2298014 100644 --- a/regions/core/tests/test_pixcoord.py +++ b/regions/core/tests/test_pixcoord.py @@ -286,3 +286,15 @@ def test_pixcoord_subtraction(): with pytest.raises(ValueError): point1 - PixCoord([1, 1, 1], [2, 2, 2]) + + +def test_pixcoord_offset_scalar(): + point = PixCoord(3, 4) + new_point = point.directional_offset_by(1, 90 * u.deg) + assert_allclose(new_point.xy, (3, 5)) + + +def test_pixcoord_offset_array(): + point = PixCoord([3, 3], [4, 4]) + new_point = point.directional_offset_by(1, 90 * u.deg) + assert_allclose(new_point.xy, ([3, 3], [5, 5])) \ No newline at end of file From b56eacc0599a95618bc02923f0ff41b45b343e95 Mon Sep 17 00:00:00 2001 From: prajwel Date: Mon, 28 Aug 2023 00:56:10 +0530 Subject: [PATCH 2/2] To fix codestyle. --- regions/core/pixcoord.py | 2 +- regions/core/tests/test_pixcoord.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/regions/core/pixcoord.py b/regions/core/pixcoord.py index 49db0c17..2ed9c5be 100644 --- a/regions/core/pixcoord.py +++ b/regions/core/pixcoord.py @@ -266,4 +266,4 @@ def directional_offset_by(self, separation, angle): """ offset_x = self.x + separation * np.cos(angle) offset_y = self.y + separation * np.sin(angle) - return PixCoord(offset_x, offset_y) \ No newline at end of file + return PixCoord(offset_x, offset_y) diff --git a/regions/core/tests/test_pixcoord.py b/regions/core/tests/test_pixcoord.py index b2298014..3339eb3d 100644 --- a/regions/core/tests/test_pixcoord.py +++ b/regions/core/tests/test_pixcoord.py @@ -297,4 +297,4 @@ def test_pixcoord_offset_scalar(): def test_pixcoord_offset_array(): point = PixCoord([3, 3], [4, 4]) new_point = point.directional_offset_by(1, 90 * u.deg) - assert_allclose(new_point.xy, ([3, 3], [5, 5])) \ No newline at end of file + assert_allclose(new_point.xy, ([3, 3], [5, 5]))