def test_angled_antiparallel(angle, offset): scale = 5 np.random.seed(19680801) # get 15 random offsets # TODO: guarantee offset > 0 results in some offsets < 0 vert_offsets = (np.random.rand(15) - offset) * scale # always start at 0 so rotation makes sense vert_offsets[0] = 0 # always take the first step the same direction vert_offsets[1] = 1 # compute points along a diagonal line x = np.sin(angle) * vert_offsets y = np.cos(angle) * vert_offsets # will check these later x_max = x[1:].max() x_min = x[1:].min() y_max = y[1:].max() y_min = y[1:].min() if offset > 0: p_expected = Path( [[0, 0], [x_max, y_max], [x_min, y_min], [x[-1], y[-1]], [0, 0]], codes=[1, 2, 2, 2, 0]) else: p_expected = Path([[0, 0], [x_max, y_max], [x[-1], y[-1]], [0, 0]], codes=[1, 2, 2, 0]) p = Path(np.vstack([x, y]).T) p2 = p.cleaned(simplify=True) assert_array_almost_equal(p_expected.vertices, p2.vertices) assert_array_equal(p_expected.codes, p2.codes)
def test_clipping_out_of_bounds(): # Should work on a Path *without* codes. path = Path([(0, 0), (1, 2), (2, 1)]) simplified = path.cleaned(clip=(10, 10, 20, 20)) assert_array_equal(simplified.vertices, [(0, 0)]) assert simplified.codes == [Path.STOP] # Should work on a Path *with* codes, and no curves. path = Path([(0, 0), (1, 2), (2, 1)], [Path.MOVETO, Path.LINETO, Path.LINETO]) simplified = path.cleaned(clip=(10, 10, 20, 20)) assert_array_equal(simplified.vertices, [(0, 0)]) assert simplified.codes == [Path.STOP] # A Path with curves does not do any clipping yet. path = Path([(0, 0), (1, 2), (2, 3)], [Path.MOVETO, Path.CURVE3, Path.CURVE3]) simplified = path.cleaned() simplified_clipped = path.cleaned(clip=(10, 10, 20, 20)) assert_array_equal(simplified.vertices, simplified_clipped.vertices) assert_array_equal(simplified.codes, simplified_clipped.codes)
def test_angled_antiparallel(angle, offset): scale = 5 np.random.seed(19680801) # get 15 random offsets # TODO: guarantee offset > 0 results in some offsets < 0 vert_offsets = (np.random.rand(15) - offset) * scale # always start at 0 so rotation makes sense vert_offsets[0] = 0 # always take the first step the same direction vert_offsets[1] = 1 # compute points along a diagonal line x = np.sin(angle) * vert_offsets y = np.cos(angle) * vert_offsets # will check these later x_max = x[1:].max() x_min = x[1:].min() y_max = y[1:].max() y_min = y[1:].min() if offset > 0: p_expected = Path([[0, 0], [x_max, y_max], [x_min, y_min], [x[-1], y[-1]], [0, 0]], codes=[1, 2, 2, 2, 0]) else: p_expected = Path([[0, 0], [x_max, y_max], [x[-1], y[-1]], [0, 0]], codes=[1, 2, 2, 0]) p = Path(np.vstack([x, y]).T) p2 = p.cleaned(simplify=True) assert_array_almost_equal(p_expected.vertices, p2.vertices) assert_array_equal(p_expected.codes, p2.codes)
#!/usr/bin/env python # coding: utf-8 # In[2]: import matplotlib.pyplot as plt from matplotlib.path import Path import matplotlib.patches as mpatches import numpy as np fig = plt.figure() ax = fig.add_subplot(111) verts = [[0, 0], [0.5, 0], [1, 0], [0, 0.5], [0.5, 0.5], [1, 0.5], [0, 1], [0.5, 1], [1, 1]] codes = [1, 1, 1, 1, 1, 1, 2, 2, 2] path = Path(verts, codes) cleaned_path = path.cleaned(simplify=True) patch = mpatches.PathPatch(cleaned_path) plt.plot(np.asarray(verts)[:, 0], np.asarray(verts)[:, 1], "rx") ax.add_patch(patch) plt.show() # In[ ]: