Skip to content

Instantly share code, notes, and snippets.

@rwarren
Created March 23, 2013 03:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rwarren/7e1b347b8b21b338bee1 to your computer and use it in GitHub Desktop.
Save rwarren/7e1b347b8b21b338bee1 to your computer and use it in GitHub Desktop.
notebook for Boundary testing matplotlib.path.Path.contains_point
{
"metadata": {
"name": "Boundary testing matplotlib.path.Path.contains_point"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "code",
"collapsed": false,
"input": "import numpy as np\nfrom matplotlib.path import Path\n#counter-clockwise path (unclosed)...\npath1 = Path([[0,0], [1,0], [1,1], [0,1]])\n#clockwise path (unclosed)...\npath2 = Path([[0,0], [0,1], [1,1], [1,0]])\n#CCW path (closed)...\npath3 = Path([[0,0], [1,0], [1,1], [0,1], [0,0]])\nall_paths = (path1, path2, path3)",
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 1
},
{
"cell_type": "code",
"collapsed": false,
"input": "#path direction seems irrelevant at first. All contain (0.5, 0.5)...\npt = (0.5, 0.5)\nfor path in all_paths:\n print path.contains_point(pt),",
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "1 1 1\n"
}
],
"prompt_number": 2
},
{
"cell_type": "code",
"collapsed": false,
"input": "#Set up a simple path testing function...\ndef test_paths(paths, test_points, r = 0.0):\n for i, path in enumerate(paths, 1):\n print \"path%d:\" % i,\n for pt in test_points:\n print path.contains_point(pt, radius=r),\n print",
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 3
},
{
"cell_type": "code",
"collapsed": false,
"input": "#Check what happens for points running up the y axis...\ny_axis_points = [[0, y] for y in np.linspace(0, 1, 11)]\ntest_paths(all_paths, y_axis_points)\n#Why is 0,0 not a hit?\n#why is y-axis not hit at all for CW path?",
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "path1: 0 1 1 1 1 1 1 1 1 1 1\npath2: 0 0 0 0 0 0 0 0 0 0 0\npath3: 0 1 1 1 1 1 1 1 1 1 1\n"
}
],
"prompt_number": 4
},
{
"cell_type": "code",
"collapsed": false,
"input": "#Check the x axis...\nx_axis_points = [[x, 0] for x in np.linspace(0, 1, 11)]\ntest_paths(all_paths, x_axis_points)\n#No hits for CW or CCW -- we clearly have to be careful with boundaries!",
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "path1: 0 0 0 0 0 0 0 0 0 0 0\npath2: 0 0 0 0 0 0 0 0 0 0 0\npath3: 0 0 0 0 0 0 0 0 0 0 0\n"
}
],
"prompt_number": 5
},
{
"cell_type": "code",
"collapsed": false,
"input": "#Check if adding a radius helps...\nr = 0.1\nprint \"X axis tests with r=%.2f\" % r\ntest_paths(all_paths, x_axis_points, r)\nprint \"\\nY axis tests with r=%.2f\" % r\ntest_paths(all_paths, y_axis_points, r)\n#strange! adding a big radius causes x-axis to be hit\n#for CCW paths, but not for the CW path!!?\n#CW path can't seem to hit on a boundary at all, even\n#with a radius.",
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "X axis tests with r=0.10\npath1: 1 1 1 1 1 1 1 1 1 1 1\npath2: 0 0 0 0 0 0 0 0 0 0 0\npath3: 1 1 1 1 1 1 1 1 1 1 1\n\nY axis tests with r=0.10\npath1: 1 1 1 1 1 1 1 1 1 1 1\npath2: 0 0 0 0 0 0 0 0 0 0 0\npath3: 1 1 1 1 1 1 1 1 1 1 1\n"
}
],
"prompt_number": 6
},
{
"cell_type": "code",
"collapsed": false,
"input": "#Sanity check a real offset by shifting test points...\nx = 1e-100\noffset_y_axis_pts = [[x, y] for y in np.linspace(0, 1, 11)]\ntest_paths(all_paths, offset_y_axis_pts, 0)\n#X axis still not detected (no surprise), and a tiny\n#offset from y axis does get detection.\n#it seems that radius is not working properly?",
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "path1: 0 1 1 1 1 1 1 1 1 1 1\npath2: 0 1 1 1 1 1 1 1 1 1 1\npath3: 0 1 1 1 1 1 1 1 1 1 1\n"
}
],
"prompt_number": 7
}
],
"metadata": {}
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment