From 344cd1fc2ab6dfd6e6a9afa369d9dfaa0a2a7967 Mon Sep 17 00:00:00 2001 From: Tim Hoffmann <2836374+timhoffm@users.noreply.github.com> Date: Thu, 31 Mar 2022 22:07:05 +0200 Subject: [PATCH] Backport PR #22628: Add RuntimeWarning guard around division-by-zero --- lib/mpl_toolkits/mplot3d/proj3d.py | 10 +++++++--- lib/mpl_toolkits/tests/test_mplot3d.py | 10 ++++++++++ 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/lib/mpl_toolkits/mplot3d/proj3d.py b/lib/mpl_toolkits/mplot3d/proj3d.py index cce89c5f3554..c9d5a7d65038 100644 --- a/lib/mpl_toolkits/mplot3d/proj3d.py +++ b/lib/mpl_toolkits/mplot3d/proj3d.py @@ -14,14 +14,18 @@ def _line2d_seg_dist(p1, p2, p0): p0[1] = y(s) intersection point p = p1 + u*(p2-p1) - and intersection point lies within segment if u is between 0 and 1 + and intersection point lies within segment if u is between 0 and 1. + + If p1 and p2 are identical, the distance between them and p0 is returned. """ - x21 = p2[0] - p1[0] - y21 = p2[1] - p1[1] x01 = np.asarray(p0[0]) - p1[0] y01 = np.asarray(p0[1]) - p1[1] + if np.all(p1 == p2): + return np.hypot(x01, y01) + x21 = p2[0] - p1[0] + y21 = p2[1] - p1[1] u = (x01*x21 + y01*y21) / (x21**2 + y21**2) u = np.clip(u, 0, 1) d = np.hypot(x01 - u*x21, y01 - u*y21) diff --git a/lib/mpl_toolkits/tests/test_mplot3d.py b/lib/mpl_toolkits/tests/test_mplot3d.py index b6abf173c1e0..ebf3e15cf124 100644 --- a/lib/mpl_toolkits/tests/test_mplot3d.py +++ b/lib/mpl_toolkits/tests/test_mplot3d.py @@ -1001,6 +1001,16 @@ def test_lines_dists(): ax.set_ylim(0, 300) +def test_lines_dists_nowarning(): + # Smoke test to see that no RuntimeWarning is emitted when two first + # arguments are the same, see GH#22624 + p0 = (10, 30) + p1 = (20, 150) + proj3d._line2d_seg_dist(p0, p0, p1) + p0 = np.array(p0) + proj3d._line2d_seg_dist(p0, p0, p1) + + def test_autoscale(): fig, ax = plt.subplots(subplot_kw={"projection": "3d"}) ax.margins(x=0, y=.1, z=.2)