fix issue#15 Path.cropped() bug, and more

-also fixed <path>.cropped(1, 0) bug (now raises AssertionError)
-also now Path.cropped() raises AssertionError if given negative values
pull/7/merge
Andy 2017-03-30 01:38:27 -07:00
parent 4ec69a4cff
commit 60d67923b6
2 changed files with 36 additions and 1 deletions

View File

@ -2136,7 +2136,13 @@ class Path(MutableSequence):
def cropped(self, T0, T1): def cropped(self, T0, T1):
"""returns a cropped copy of the path.""" """returns a cropped copy of the path."""
assert 0 <= T0 <= 1 and 0 <= T1<= 1
assert T0 != T1 assert T0 != T1
assert not (T0 == 1 and T1 == 0)
if T0 == 1 and 0 < T1 < 1 and self.isclosed():
return self.cropped(0, T1)
if T1 == 1: if T1 == 1:
seg1 = self[-1] seg1 = self[-1]
t_seg1 = 1 t_seg1 = 1
@ -2171,7 +2177,7 @@ class Path(MutableSequence):
# T1<T0 must cross discontinuity case # T1<T0 must cross discontinuity case
if T1 < T0: if T1 < T0:
if self.isclosed(): if not self.isclosed():
raise ValueError("This path is not closed, thus T0 must " raise ValueError("This path is not closed, thus T0 must "
"be less than T1.") "be less than T1.")
else: else:

View File

@ -660,6 +660,35 @@ class TestPath(unittest.TestCase):
self.assertTrue(chk1) self.assertTrue(chk1)
self.assertTrue(chk2) self.assertTrue(chk2)
def test_cropped(self):
p_closed = Path(Line(0, 1), Line(1, 1 + 1j), Line(1 + 1j, 1j),
Line(1j, 0))
first_half = Path(Line(0, 1), Line(1, 1 + 1j))
second_half = Path(Line(1 + 1j, 1j), Line(1j, 0))
middle_half = Path(Line(1, 1 + 1j), Line(1 + 1j, 1j))
other_middle_half = Path(Line(1j, 0), Line(0, 1))
self.assertTrue(p_closed.cropped(0, 0.5) == first_half)
self.assertTrue(p_closed.cropped(1, 0.5) == first_half)
self.assertTrue(p_closed.cropped(.5, 1) == second_half)
self.assertTrue(p_closed.cropped(0.25, 0.75) == middle_half)
self.assertTrue(p_closed.cropped(0.75, 0.25) == other_middle_half)
with self.assertRaises(AssertionError):
p_closed.cropped(1, 0)
with self.assertRaises(AssertionError):
p_closed.cropped(.5, 1.1)
with self.assertRaises(AssertionError):
p_closed.cropped(-0.1, 0.1)
p_open = Path(Line(0, 1), Line(1, 1 + 1j), Line(1 + 1j, 1j),
Line(1j, 2j))
self.assertTrue(p_open.cropped(0, 0.5) == first_half)
with self.assertRaises(ValueError):
p_open.cropped(.75, .25)
with self.assertRaises(ValueError):
p_open.cropped(1, .25)
with self.assertRaises(AssertionError):
p_open.cropped(1, 0)
class Test_ilength(unittest.TestCase): class Test_ilength(unittest.TestCase):
# See svgpathtools.notes.inv_arclength.py for information on how these # See svgpathtools.notes.inv_arclength.py for information on how these