Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1cf4f5e

Browse files
committed
Merge pull request #1787 from mdboom/points_in_path_bug
Path.contains_points() incorrect return
2 parents eae5e43 + dae730e commit 1cf4f5e

File tree

2 files changed

+26
-4
lines changed

2 files changed

+26
-4
lines changed

lib/matplotlib/tests/test_artist.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
from __future__ import print_function
22

3+
import numpy as np
4+
35
from matplotlib.testing.decorators import cleanup
46

57
import matplotlib.pyplot as plt
@@ -88,6 +90,19 @@ def test_collection_transform_of_none():
8890
assert isinstance(c._transOffset, mtrans.IdentityTransform)
8991

9092

93+
def test_point_in_path():
94+
from matplotlib.path import Path
95+
96+
# Test #1787
97+
verts2 = [(0,0), (0,1), (1,1), (1,0), (0,0)]
98+
99+
path = Path(verts2, closed=True)
100+
points = [(0.5,0.5), (1.5,0.5)]
101+
102+
assert np.all(path.contains_points(points) == [True, False])
103+
104+
105+
91106
if __name__=='__main__':
92107
import nose
93108
nose.runmodule(argv=['-s','--with-doctest'], exit=False)

src/_path.cpp

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ point_in_path_impl(const void* const points_, const size_t s0,
128128
npy_bool* const inside_flag)
129129
{
130130
int *yflag0;
131+
int *subpath_flag;
131132
int yflag1;
132133
double vtx0, vty0, vtx1, vty1;
133134
double tx, ty;
@@ -138,6 +139,7 @@ point_in_path_impl(const void* const points_, const size_t s0,
138139
const char *const points = (const char * const)points_;
139140

140141
yflag0 = (int *)malloc(n * sizeof(int));
142+
subpath_flag = (int *)malloc(n * sizeof(int));
141143

142144
path.rewind(0);
143145

@@ -151,6 +153,10 @@ point_in_path_impl(const void* const points_, const size_t s0,
151153
if (code != agg::path_cmd_move_to)
152154
{
153155
code = path.vertex(&x, &y);
156+
if (code == agg::path_cmd_stop ||
157+
(code & agg::path_cmd_end_poly) == agg::path_cmd_end_poly) {
158+
continue;
159+
}
154160
}
155161

156162
sx = vtx0 = vtx1 = x;
@@ -162,7 +168,7 @@ point_in_path_impl(const void* const points_, const size_t s0,
162168
// get test bit for above/below X axis
163169
yflag0[i] = (vty0 >= ty);
164170

165-
inside_flag[i] = 0;
171+
subpath_flag[i] = 0;
166172
}
167173

168174
do
@@ -208,7 +214,7 @@ point_in_path_impl(const void* const points_, const size_t s0,
208214
// tests.
209215
if (((vty1 - ty) * (vtx0 - vtx1) >=
210216
(vtx1 - tx) * (vty0 - vty1)) == yflag1) {
211-
inside_flag[i] ^= 1;
217+
subpath_flag[i] ^= 1;
212218
}
213219
}
214220

@@ -235,10 +241,10 @@ point_in_path_impl(const void* const points_, const size_t s0,
235241
if (yflag0[i] != yflag1) {
236242
if (((vty1 - ty) * (vtx0 - vtx1) >=
237243
(vtx1 - tx) * (vty0 - vty1)) == yflag1) {
238-
inside_flag[i] ^= 1;
244+
subpath_flag[i] ^= 1;
239245
}
240246
}
241-
247+
inside_flag[i] |= subpath_flag[i];
242248
if (inside_flag[i] == 0) {
243249
all_done = 0;
244250
}
@@ -253,6 +259,7 @@ point_in_path_impl(const void* const points_, const size_t s0,
253259
exit:
254260

255261
free(yflag0);
262+
free(subpath_flag);
256263
}
257264

258265
inline void

0 commit comments

Comments
 (0)