Skip to content

Commit 4b63f75

Browse files
committed
Skip points with NaNs in point_in_path and friends
1 parent f52b90b commit 4b63f75

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

lib/matplotlib/tests/test_path.py

+7
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,13 @@ def test_path_clipping():
6161
xy, facecolor='none', edgecolor='red', closed=True))
6262

6363

64+
def test_point_in_path_nan():
65+
box = np.array([[0, 0], [1, 0], [1, 1], [0, 1], [0, 0]])
66+
p = Path(box)
67+
test = np.array([[np.nan, 0.5]])
68+
assert p.contains_points(test)[0] == False
69+
70+
6471
if __name__ == '__main__':
6572
import nose
6673
nose.runmodule(argv=['-s', '--with-doctest'], exit=False)

src/_path.cpp

+13-3
Original file line numberDiff line numberDiff line change
@@ -165,10 +165,12 @@ point_in_path_impl(const void* const points_, const size_t s0,
165165
for (i = 0; i < n; ++i) {
166166
ty = *(double *)(points + s0 * i + s1);
167167

168-
// get test bit for above/below X axis
169-
yflag0[i] = (vty0 >= ty);
168+
if (MPL_isfinite64(ty)) {
169+
// get test bit for above/below X axis
170+
yflag0[i] = (vty0 >= ty);
170171

171-
subpath_flag[i] = 0;
172+
subpath_flag[i] = 0;
173+
}
172174
}
173175

174176
do
@@ -191,6 +193,10 @@ point_in_path_impl(const void* const points_, const size_t s0,
191193
tx = *(double *)(points + s0 * i);
192194
ty = *(double *)(points + s0 * i + s1);
193195

196+
if (MPL_notisfinite64(tx) || MPL_notisfinite64(ty)) {
197+
continue;
198+
}
199+
194200
yflag1 = (vty1 >= ty);
195201
// Check if endpoints straddle (are on opposite sides) of
196202
// X axis (i.e. the Y's differ); if so, +X ray could
@@ -237,6 +243,10 @@ point_in_path_impl(const void* const points_, const size_t s0,
237243
tx = *(double *)(points + s0 * i);
238244
ty = *(double *)(points + s0 * i + s1);
239245

246+
if (MPL_notisfinite64(tx) || MPL_notisfinite64(ty)) {
247+
continue;
248+
}
249+
240250
yflag1 = (vty1 >= ty);
241251
if (yflag0[i] != yflag1) {
242252
if (((vty1 - ty) * (vtx0 - vtx1) >=

0 commit comments

Comments
 (0)