Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added directional_offset_by function to PixCoord #507

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions regions/core/pixcoord.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
12 changes: 12 additions & 0 deletions regions/core/tests/test_pixcoord.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]))