Skip to content

Commit e6e20e3

Browse files
authored
Merge pull request #22628 from oscargus/proj3derrorstate
Add RuntimeWarning guard around division-by-zero
2 parents a5fec21 + 503d7b9 commit e6e20e3

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

lib/mpl_toolkits/mplot3d/proj3d.py

+7-3
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,18 @@ def _line2d_seg_dist(p1, p2, p0):
1414
p0[1] = y(s)
1515
1616
intersection point p = p1 + u*(p2-p1)
17-
and intersection point lies within segment if u is between 0 and 1
17+
and intersection point lies within segment if u is between 0 and 1.
18+
19+
If p1 and p2 are identical, the distance between them and p0 is returned.
1820
"""
1921

20-
x21 = p2[0] - p1[0]
21-
y21 = p2[1] - p1[1]
2222
x01 = np.asarray(p0[0]) - p1[0]
2323
y01 = np.asarray(p0[1]) - p1[1]
24+
if np.all(p1 == p2):
25+
return np.hypot(x01, y01)
2426

27+
x21 = p2[0] - p1[0]
28+
y21 = p2[1] - p1[1]
2529
u = (x01*x21 + y01*y21) / (x21**2 + y21**2)
2630
u = np.clip(u, 0, 1)
2731
d = np.hypot(x01 - u*x21, y01 - u*y21)

lib/mpl_toolkits/tests/test_mplot3d.py

+10
Original file line numberDiff line numberDiff line change
@@ -1002,6 +1002,16 @@ def test_lines_dists():
10021002
ax.set_ylim(0, 300)
10031003

10041004

1005+
def test_lines_dists_nowarning():
1006+
# Smoke test to see that no RuntimeWarning is emitted when two first
1007+
# arguments are the same, see GH#22624
1008+
p0 = (10, 30)
1009+
p1 = (20, 150)
1010+
proj3d._line2d_seg_dist(p0, p0, p1)
1011+
p0 = np.array(p0)
1012+
proj3d._line2d_seg_dist(p0, p0, p1)
1013+
1014+
10051015
def test_autoscale():
10061016
fig, ax = plt.subplots(subplot_kw={"projection": "3d"})
10071017
ax.margins(x=0, y=.1, z=.2)

0 commit comments

Comments
 (0)