Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Path.contains_points() incorrect return #1787

Merged
merged 2 commits into from May 3, 2013
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 15 additions & 0 deletions lib/matplotlib/tests/test_artist.py
@@ -1,5 +1,7 @@
from __future__ import print_function

import numpy as np

from matplotlib.testing.decorators import cleanup

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


def test_point_in_path():
from matplotlib.path import Path

# Test #1787
verts2 = [(0,0), (0,1), (1,1), (1,0), (0,0)]

path = Path(verts2, closed=True)
points = [(0.5,0.5), (1.5,0.5)]

assert np.all(path.contains_points(points) == [True, False])



if __name__=='__main__':
import nose
nose.runmodule(argv=['-s','--with-doctest'], exit=False)
15 changes: 11 additions & 4 deletions src/_path.cpp
Expand Up @@ -128,6 +128,7 @@ point_in_path_impl(const void* const points_, const size_t s0,
npy_bool* const inside_flag)
{
int *yflag0;
int *subpath_flag;
int yflag1;
double vtx0, vty0, vtx1, vty1;
double tx, ty;
Expand All @@ -138,6 +139,7 @@ point_in_path_impl(const void* const points_, const size_t s0,
const char *const points = (const char * const)points_;

yflag0 = (int *)malloc(n * sizeof(int));
subpath_flag = (int *)malloc(n * sizeof(int));

path.rewind(0);

Expand All @@ -151,6 +153,10 @@ point_in_path_impl(const void* const points_, const size_t s0,
if (code != agg::path_cmd_move_to)
{
code = path.vertex(&x, &y);
if (code == agg::path_cmd_stop ||
(code & agg::path_cmd_end_poly) == agg::path_cmd_end_poly) {
continue;
}
}

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

inside_flag[i] = 0;
subpath_flag[i] = 0;
}

do
Expand Down Expand Up @@ -208,7 +214,7 @@ point_in_path_impl(const void* const points_, const size_t s0,
// tests.
if (((vty1 - ty) * (vtx0 - vtx1) >=
(vtx1 - tx) * (vty0 - vty1)) == yflag1) {
inside_flag[i] ^= 1;
subpath_flag[i] ^= 1;
}
}

Expand All @@ -235,10 +241,10 @@ point_in_path_impl(const void* const points_, const size_t s0,
if (yflag0[i] != yflag1) {
if (((vty1 - ty) * (vtx0 - vtx1) >=
(vtx1 - tx) * (vty0 - vty1)) == yflag1) {
inside_flag[i] ^= 1;
subpath_flag[i] ^= 1;
}
}

inside_flag[i] |= subpath_flag[i];
if (inside_flag[i] == 0) {
all_done = 0;
}
Expand All @@ -253,6 +259,7 @@ point_in_path_impl(const void* const points_, const size_t s0,
exit:

free(yflag0);
free(subpath_flag);
}

inline void
Expand Down