Skip to content

Commit b905689

Browse files
committed
polygon vertices can be a list of tuples
don't use closed=False, intersection doesn't work properly
1 parent aef0c1b commit b905689

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

spatialmath/geom2d.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,23 @@ def __init__(self, vertices=None):
4949
.. note:: The polygon is represented by a Matplotlib ``Path``
5050
"""
5151

52-
if vertices is not None:
53-
vertices = np.array(vertices)
54-
self.path = Path(vertices.T, closed=False)
55-
self.path0 = self.path
52+
if isinstance(vertices, (list, tuple)):
53+
vertices = np.array(vertices).T
54+
elif isinstance(vertices, np.ndarray):
55+
if vertices.shape[0] != 2:
56+
raise ValueError('ndarray must be 2xN')
57+
elif vertices is None:
58+
return
59+
else:
60+
raise TypeError('expecting list of 2-tuples or ndarray(2,N)')
61+
62+
# replicate the first vertex to make it closed.
63+
# setting closed=False and codes=None leads to a different
64+
# path which gives incorrect intersection results
65+
vertices = np.hstack((vertices, vertices[:, 0:1]))
66+
67+
self.path = Path(vertices.T, closed=True)
68+
self.path0 = self.path
5669

5770
def __str__(self):
5871
"""

0 commit comments

Comments
 (0)