import numpy as np
from matplotlib.path import Path
#counter-clockwise path (unclosed)...
path1 = Path([[0,0], [1,0], [1,1], [0,1]])
#clockwise path (unclosed)...
path2 = Path([[0,0], [0,1], [1,1], [1,0]])
#CCW path (closed)...
path3 = Path([[0,0], [1,0], [1,1], [0,1], [0,0]])
all_paths = (path1, path2, path3)
#path direction seems irrelevant at first. All contain (0.5, 0.5)...
pt = (0.5, 0.5)
for path in all_paths:
print path.contains_point(pt),
1 1 1
#Set up a simple path testing function...
def test_paths(paths, test_points, r = 0.0):
for i, path in enumerate(paths, 1):
print "path%d:" % i,
for pt in test_points:
print path.contains_point(pt, radius=r),
print
#Check what happens for points running up the y axis...
y_axis_points = [[0, y] for y in np.linspace(0, 1, 11)]
test_paths(all_paths, y_axis_points)
#Why is 0,0 not a hit?
#why is y-axis not hit at all for CW path?
path1: 0 1 1 1 1 1 1 1 1 1 1 path2: 0 0 0 0 0 0 0 0 0 0 0 path3: 0 1 1 1 1 1 1 1 1 1 1
#Check the x axis...
x_axis_points = [[x, 0] for x in np.linspace(0, 1, 11)]
test_paths(all_paths, x_axis_points)
#No hits for CW or CCW -- we clearly have to be careful with boundaries!
path1: 0 0 0 0 0 0 0 0 0 0 0 path2: 0 0 0 0 0 0 0 0 0 0 0 path3: 0 0 0 0 0 0 0 0 0 0 0
#Check if adding a radius helps...
r = 0.1
print "X axis tests with r=%.2f" % r
test_paths(all_paths, x_axis_points, r)
print "\nY axis tests with r=%.2f" % r
test_paths(all_paths, y_axis_points, r)
#strange! adding a big radius causes x-axis to be hit
#for CCW paths, but not for the CW path!!?
#CW path can't seem to hit on a boundary at all, even
#with a radius.
X axis tests with r=0.10 path1: 1 1 1 1 1 1 1 1 1 1 1 path2: 0 0 0 0 0 0 0 0 0 0 0 path3: 1 1 1 1 1 1 1 1 1 1 1 Y axis tests with r=0.10 path1: 1 1 1 1 1 1 1 1 1 1 1 path2: 0 0 0 0 0 0 0 0 0 0 0 path3: 1 1 1 1 1 1 1 1 1 1 1
#Sanity check a real offset by shifting test points...
x = 1e-100
offset_y_axis_pts = [[x, y] for y in np.linspace(0, 1, 11)]
test_paths(all_paths, offset_y_axis_pts, 0)
#X axis still not detected (no surprise), and a tiny
#offset from y axis does get detection.
#it seems that radius is not working properly?
path1: 0 1 1 1 1 1 1 1 1 1 1 path2: 0 1 1 1 1 1 1 1 1 1 1 path3: 0 1 1 1 1 1 1 1 1 1 1