8000 Changed handling of division by zero · matplotlib/matplotlib@ee10314 · GitHub
[go: up one dir, main page]

Skip to content

Commit ee10314

Browse files
committed
Changed handling of division by zero
1 parent 54ec353 commit ee10314

File tree

2 files changed

+21
-6
lines changed

2 files changed

+21
-6
lines changed

lib/matplotlib/bezier.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,8 @@ def find_bezier_t_intersecting_with_closedpath(bezier_point_at_t,
124124
start_inside = inside_closedpath(start)
125125
end_inside = inside_closedpath(end)
126126

127-
if not xor(start_inside, end_inside):
128-
if (start != end):
127+
if start_inside == end_inside:
128+
if start != end:
129129
raise NonIntersectingPathException(
130130
"the segment does not seem to intersect with the path"
131131
)
@@ -317,7 +317,10 @@ def _f(xy):
317317

318318
def get_cos_sin(x0, y0, x1, y1):
319319
dx, dy = x1 - x0, y1 - y0
320-
d = (dx * dx + dy * dy) ** .5 or 1 # Account for divide by zero
320+
d = (dx * dx + dy * dy) ** .5
321+
# Account for divide by zero
322+
if d == 0:
323+
return 0.0, 0.0
321324
return dx / d, dy / d
322325

323326

lib/matplotlib/patches.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1049,7 +1049,12 @@ def __init__(self, x, y, dx, dy, width=1.0, **kwargs):
10491049
%(Patch)s
10501050
"""
10511051
Patch.__init__(self, **kwargs)
1052-
L = np.sqrt(dx ** 2 + dy ** 2) or 1 # account for div by zero
1052+
L = np.sqrt(dx ** 2 + dy ** 2)
1053+
1054+
# Account for divide by zero
1055+
if L == 0:
1056+
L = 1
1057+
10531058
cx = float(dx) / L
10541059
sx = float(dy) / L
10551060

@@ -1112,8 +1117,12 @@ def __init__(self, x, y, dx, dy, width=0.001, length_includes_head=False,
11121117
if head_length is None:
11131118
head_length = 1.5 * head_width
11141119

1120+
distance = np.sqrt(dx ** 2 + dy ** 2)
1121+
11151122
# Account for divide by zero
1116-
distance = np.sqrt(dx ** 2 + dy ** 2) or 1
1123+
if distance == 0:
1124+
distance = 1
1125+
11171126
if length_includes_head:
11181127
length = distance
11191128
else:
@@ -3201,8 +3210,11 @@ def _get_arrow_wedge(self, x0, y0, x1, y1,
32013210
# arrow from x0, y0 to x1, y1
32023211
dx, dy = x0 - x1, y0 - y1
32033212

3213+
cp_distance = math.sqrt(dx ** 2 + dy ** 2)
3214+
32043215
# Account for divide by zero
3205-
cp_distance = math.sqrt(dx ** 2 + dy ** 2) or 1
3216+
if cp_distance == 0:
3217+
cp_distance = 1
32063218

32073219
# pad_projected : amount of pad to account the
32083220
# overshooting of the projection of the wedge

0 commit comments

Comments
 (0)
0