Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions lib/mpl_toolkits/mplot3d/proj3d.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
10 changes: 10 additions & 0 deletions lib/mpl_toolkits/tests/test_mplot3d.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down