Skip to content

Commit

Permalink
Merge pull request #4 from mdickinson/polygon-numpy-compatibility
Browse files Browse the repository at this point in the history
Make Polygon work correctly with NumPy arrays.
  • Loading branch information
mdickinson authored Jan 14, 2019
2 parents 527fd58 + 65173d8 commit 87c3c71
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
13 changes: 11 additions & 2 deletions polygon.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,12 @@ def sign(x):
Return 1 if x is positive, -1 if it's negative, and 0 if it's zero.
"""
return (x > 0) - (x < 0)
if x > 0:
return 1
elif x < 0:
return -1
else:
return 0


def vertex_sign(P, O):
Expand Down Expand Up @@ -80,7 +85,11 @@ def edge_positions(self):
"""
points = self.vertex_positions
return zip(points, points[1:] + points[:1])
first_point = previous_point = points[0]
for point in points[1:]:
yield previous_point, point
previous_point = point
yield previous_point, first_point

def area(self):
"""
Expand Down
24 changes: 24 additions & 0 deletions test_polygon.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@
else:
import unittest2 as unittest

try:
import numpy
except ImportError:
NUMPY_AVAILABLE = False
else:
NUMPY_AVAILABLE = True

from polygon import Polygon


Expand Down Expand Up @@ -124,3 +131,20 @@ def test_aitch(self):
else:
with self.assertRaises(ValueError):
aitch.winding_number(point)

@unittest.skipUnless(NUMPY_AVAILABLE, "Test requires NumPy")
def test_numpy_compatibility(self):
square = Polygon(
vertex_positions=numpy.array(
[
[1.0, -1.0],
[1.0, 1.0],
[-1.0, 1.0],
[-1.0, -1.0],
],
dtype=numpy.float64,
)
)
origin = numpy.array([0.0, 0.0], dtype=numpy.float64)
self.assertEqual(square.winding_number(origin), 1)
self.assertEqual(square.area(), 4.0)

0 comments on commit 87c3c71

Please sign in to comment.