Skip to content

Commit 9e1888c

Browse files
committed
Handle CLOSEPOLY's in Path.interpolated
1 parent a4e5623 commit 9e1888c

File tree

3 files changed

+32
-5
lines changed

3 files changed

+32
-5
lines changed
Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
``Path.interpolated`` now handles internal ``MOVETO``'s
2-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1+
``Path.interpolated`` now handles ``CLOSEPOLY``'s and internal ``MOVETO``'s
2+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
33

44
If `.Path` contains internal `~.Path.MOVETO` codes, the `~.Path.interpolated` method
5-
is now applied to each sub-path delineated by those codes.
5+
is now applied to each sub-path delineated by those codes. `~.Path.CLOSEPOLY` is
6+
also now correctly handled.

lib/matplotlib/path.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -669,7 +669,7 @@ def interpolated(self, steps):
669669
"""
670670
Return a new path with each segment divided into *steps* parts.
671671
672-
Codes other than `LINETO` and `MOVETO` are not handled correctly.
672+
Codes other than `LINETO` `MOVETO` and `CLOSEPOLY` are not handled correctly.
673673
674674
Parameters
675675
----------
@@ -688,7 +688,13 @@ def interpolated(self, steps):
688688
return self.make_compound_path(
689689
*(p.interpolated(steps) for p in self._iter_connected_components()))
690690

691-
vertices = simple_linear_interpolation(self.vertices, steps)
691+
if self.codes[-1] == self.CLOSEPOLY and not np.all(self.vertices[-1] ==
692+
self.vertices[0]):
693+
vertices = np.concatenate([self.vertices[:-1], self.vertices[:1]], axis=0)
694+
else:
695+
vertices = self.vertices
696+
697+
vertices = simple_linear_interpolation(vertices, steps)
692698
codes = self.codes
693699
if codes is not None:
694700
new_codes = np.full((len(codes) - 1) * steps + 1, Path.LINETO,

lib/matplotlib/tests/test_path.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -559,3 +559,23 @@ def test_interpolated_moveto():
559559
# Result should have two subpaths with six LINETOs each
560560
expected_subpath_codes = [Path.MOVETO] + [Path.LINETO] * 6
561561
np.testing.assert_array_equal(result.codes, expected_subpath_codes * 2)
562+
563+
564+
def test_interpolated_closepoly():
565+
codes = [Path.MOVETO] + [Path.LINETO]*2 + [Path.CLOSEPOLY]
566+
vertices = [(4, 4), (5, 5), (5, 4), (0, 0)]
567+
568+
path = Path(vertices, codes)
569+
result = path.interpolated(2)
570+
571+
expected_vertices = np.array([[4, 4],
572+
[4.5, 4.5],
573+
[5, 5],
574+
[5, 4.5],
575+
[5, 4],
576+
[4.5, 4],
577+
[4, 4]])
578+
expected_codes = [Path.MOVETO] + [Path.LINETO]*5 + [Path.CLOSEPOLY]
579+
580+
np.testing.assert_allclose(result.vertices, expected_vertices)
581+
np.testing.assert_array_equal(result.codes, expected_codes)

0 commit comments

Comments
 (0)