Skip to content

Commit 73622a0

Browse files
committed
Simplify some patches path definitions.
- When a Path ends with a CLOSEPOLY, it is not necessary to put a LINETO to the closing position before it (in fact that can result in an incorrect line join at that position), and the xy position associated with the CLOSEPOLY can just be (0, 0), as it is irrelevant. - For defining the codes arrays, for short paths (such as the patch shapes here), one can just use list unpacking for shorter definitions. - Rename the _path and _fillable lists in ArrowStyle to plural names. - Rely on the default tolerance of split_bezier_intersecting_with_closedpath (which is 0.01) rather than re-specifying the same magic value everywhere. - Remove inapplicable comment re: make_compound_path_from_polys (which only applies to polygons all of with the same number of sides, which is not the case when clipping to a bbox).
1 parent 9b4985e commit 73622a0

File tree

2 files changed

+29
-46
lines changed

2 files changed

+29
-46
lines changed

lib/matplotlib/patches.py

Lines changed: 29 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1209,21 +1209,15 @@ def _recompute_path(self):
12091209
# followed by a reversed and scaled inner ring
12101210
v1 = arc.vertices
12111211
v2 = arc.vertices[::-1] * (self.r - self.width) / self.r
1212-
v = np.concatenate([v1, v2, [v1[0, :], (0, 0)]])
1213-
c = np.concatenate([
1214-
arc.codes, arc.codes, [connector, Path.CLOSEPOLY]])
1215-
c[len(arc.codes)] = connector
1212+
v = np.concatenate([v1, v2, [(0, 0)]])
1213+
c = [*arc.codes, connector, *arc.codes[1:], Path.CLOSEPOLY]
12161214
else:
12171215
# Wedge doesn't need an inner ring
1218-
v = np.concatenate([
1219-
arc.vertices, [(0, 0), arc.vertices[0, :], (0, 0)]])
1220-
c = np.concatenate([
1221-
arc.codes, [connector, connector, Path.CLOSEPOLY]])
1216+
v = np.concatenate([arc.vertices, [(0, 0), (0, 0)]])
1217+
c = [*arc.codes, connector, Path.CLOSEPOLY]
12221218

12231219
# Shift and scale the wedge to the final location.
1224-
v *= self.r
1225-
v += np.asarray(self.center)
1226-
self._path = Path(v, c)
1220+
self._path = Path(v * self.r + self.center, c)
12271221

12281222
def set_center(self, center):
12291223
self._path = None
@@ -2480,9 +2474,7 @@ def __call__(self, x0, y0, width, height, mutation_size):
24802474
Path.CURVE3, Path.CURVE3,
24812475
Path.CLOSEPOLY]
24822476

2483-
path = Path(cp, com)
2484-
2485-
return path
2477+
return Path(cp, com)
24862478

24872479
@_register_style(_style_list)
24882480
class Round4:
@@ -2531,9 +2523,7 @@ def __call__(self, x0, y0, width, height, mutation_size):
25312523
Path.CURVE4, Path.CURVE4, Path.CURVE4,
25322524
Path.CLOSEPOLY]
25332525

2534-
path = Path(cp, com)
2535-
2536-
return path
2526+
return Path(cp, com)
25372527

25382528
@_register_style(_style_list)
25392529
class Sawtooth:
@@ -2628,8 +2618,7 @@ def _get_sawtooth_vertices(self, x0, y0, width, height, mutation_size):
26282618
def __call__(self, x0, y0, width, height, mutation_size):
26292619
saw_vertices = self._get_sawtooth_vertices(x0, y0, width,
26302620
height, mutation_size)
2631-
path = Path(saw_vertices, closed=True)
2632-
return path
2621+
return Path(saw_vertices, closed=True)
26332622

26342623
@_register_style(_style_list)
26352624
class Roundtooth(Sawtooth):
@@ -3419,22 +3408,20 @@ def transmute(self, path, mutation_size, linewidth):
34193408

34203409
# This simple code will not work if ddx, ddy is greater than the
34213410
# separation between vertices.
3422-
_path = [Path(np.concatenate([[(x0 + ddxA, y0 + ddyA)],
3411+
paths = [Path(np.concatenate([[(x0 + ddxA, y0 + ddyA)],
34233412
path.vertices[1:-1],
34243413
[(x3 + ddxB, y3 + ddyB)]]),
34253414
path.codes)]
3426-
_fillable = [False]
3415+
fills = [False]
34273416

34283417
if has_begin_arrow:
34293418
if self.fillbegin:
3430-
p = np.concatenate([verticesA, [verticesA[0],
3431-
verticesA[0]], ])
3432-
c = np.concatenate([codesA, [Path.LINETO, Path.CLOSEPOLY]])
3433-
_path.append(Path(p, c))
3434-
_fillable.append(True)
3419+
paths.append(
3420+
Path([*verticesA, (0, 0)], [*codesA, Path.CLOSEPOLY]))
3421+
fills.append(True)
34353422
else:
3436-
_path.append(Path(verticesA, codesA))
3437-
_fillable.append(False)
3423+
paths.append(Path(verticesA, codesA))
3424+
fills.append(False)
34383425
elif self._beginarrow_bracket:
34393426
x0, y0 = path.vertices[0]
34403427
x1, y1 = path.vertices[1]
@@ -3443,19 +3430,17 @@ def transmute(self, path, mutation_size, linewidth):
34433430
self.lengthA * scaleA,
34443431
self.angleA)
34453432

3446-
_path.append(Path(verticesA, codesA))
3447-
_fillable.append(False)
3433+
paths.append(Path(verticesA, codesA))
3434+
fills.append(False)
34483435

34493436
if has_end_arrow:
34503437
if self.fillend:
3451-
_fillable.append(True)
3452-
p = np.concatenate([verticesB, [verticesB[0],
3453-
verticesB[0]], ])
3454-
c = np.concatenate([codesB, [Path.LINETO, Path.CLOSEPOLY]])
3455-
_path.append(Path(p, c))
3438+
fills.append(True)
3439+
paths.append(
3440+
Path([*verticesB, (0, 0)], [*codesB, Path.CLOSEPOLY]))
34563441
else:
3457-
_fillable.append(False)
3458-
_path.append(Path(verticesB, codesB))
3442+
fills.append(False)
3443+
paths.append(Path(verticesB, codesB))
34593444
elif self._endarrow_bracket:
34603445
x0, y0 = path.vertices[-1]
34613446
x1, y1 = path.vertices[-2]
@@ -3464,10 +3449,10 @@ def transmute(self, path, mutation_size, linewidth):
34643449
self.lengthB * scaleB,
34653450
self.angleB)
34663451

3467-
_path.append(Path(verticesB, codesB))
3468-
_fillable.append(False)
3452+
paths.append(Path(verticesB, codesB))
3453+
fills.append(False)
34693454

3470-
return _path, _fillable
3455+
return paths, fills
34713456

34723457
@_register_style(_style_list, name="-")
34733458
class Curve(_Curve):
@@ -3662,8 +3647,7 @@ def transmute(self, path, mutation_size, linewidth):
36623647

36633648
try:
36643649
arrow_out, arrow_in = \
3665-
split_bezier_intersecting_with_closedpath(
3666-
arrow_path, in_f, tolerance=0.01)
3650+
split_bezier_intersecting_with_closedpath(arrow_path, in_f)
36673651
except NonIntersectingPathException:
36683652
# if this happens, make a straight line of the head_length
36693653
# long.
@@ -3743,7 +3727,7 @@ def transmute(self, path, mutation_size, linewidth):
37433727
in_f = inside_circle(x2, y2, head_length)
37443728
try:
37453729
path_out, path_in = split_bezier_intersecting_with_closedpath(
3746-
arrow_path, in_f, tolerance=0.01)
3730+
arrow_path, in_f)
37473731
except NonIntersectingPathException:
37483732
# if this happens, make a straight line of the head_length
37493733
# long.
@@ -3757,7 +3741,7 @@ def transmute(self, path, mutation_size, linewidth):
37573741
# path for head
37583742
in_f = inside_circle(x2, y2, head_length * .8)
37593743
path_out, path_in = split_bezier_intersecting_with_closedpath(
3760-
arrow_path, in_f, tolerance=0.01)
3744+
arrow_path, in_f)
37613745
path_tail = path_out
37623746

37633747
# head
@@ -3775,7 +3759,7 @@ def transmute(self, path, mutation_size, linewidth):
37753759
# path for head
37763760
in_f = inside_circle(x0, y0, tail_width * .3)
37773761
path_in, path_out = split_bezier_intersecting_with_closedpath(
3778-
arrow_path, in_f, tolerance=0.01)
3762+
arrow_path, in_f)
37793763
tail_start = path_in[-1]
37803764

37813765
head_right, head_left = head_r, head_l

lib/matplotlib/path.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1040,7 +1040,6 @@ def clip_to_bbox(self, bbox, inside=True):
10401040
If *inside* is `True`, clip to the inside of the box, otherwise
10411041
to the outside of the box.
10421042
"""
1043-
# Use make_compound_path_from_polys
10441043
verts = _path.clip_path_to_rect(self, bbox, inside)
10451044
paths = [Path(poly) for poly in verts]
10461045
return self.make_compound_path(*paths)

0 commit comments

Comments
 (0)