Skip to content

Commit

Permalink
Merge 8a3ff74 into ffdf4d4
Browse files Browse the repository at this point in the history
  • Loading branch information
cdeil committed Jul 3, 2019
2 parents ffdf4d4 + 8a3ff74 commit 6177abb
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
12 changes: 11 additions & 1 deletion regions/shapes/polygon.py
Expand Up @@ -59,7 +59,17 @@ def __init__(self, vertices, meta=None, visual=None):
@property
def area(self):
"""Region area (float)."""
raise NotImplementedError
# See https://stackoverflow.com/questions/24467972

# Use offsets to improve numerical precision
x_ = self.vertices.x - self.vertices.x.mean()
y_ = self.vertices.y - self.vertices.y.mean()

# Shoelace formula, for our case where the start vertex
# isn't duplicated at the end, written to avoid an array copy
area_main = np.dot(x_[:-1], y_[1:]) - np.dot(y_[:-1], x_[1:])
area_last = x_[-1] * y_[0] - y_[-1] * x_[0]
return 0.5 * np.abs(area_main + area_last)

def contains(self, pixcoord):
pixcoord = PixCoord._validate(pixcoord, 'pixcoord')
Expand Down
5 changes: 0 additions & 5 deletions regions/shapes/tests/test_common.py
Expand Up @@ -7,7 +7,6 @@
from numpy.testing import assert_equal, assert_allclose
import pytest

from ..polygon import PolygonPixelRegion
from ...core import PixCoord, BoundingBox

class BaseTestRegion(object):
Expand All @@ -22,10 +21,6 @@ def test_str(self):
class BaseTestPixelRegion(BaseTestRegion):

def test_area(self):
# TODO: remove the pytest.skip once polygon area is implemented
if isinstance(self.reg, PolygonPixelRegion):
pytest.skip()

assert_allclose(self.reg.area, self.expected_area)

def test_mask_area(self):
Expand Down

0 comments on commit 6177abb

Please sign in to comment.