diff --git a/regions/core/pixcoord.py b/regions/core/pixcoord.py index 8d938666..2ed9c5be 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) diff --git a/regions/core/tests/test_pixcoord.py b/regions/core/tests/test_pixcoord.py index 096a6cf4..3339eb3d 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]))