Skip to content

Instantly share code, notes, and snippets.

@subnivean
Created December 11, 2012 03:21
Show Gist options
  • Save subnivean/4255679 to your computer and use it in GitHub Desktop.
Save subnivean/4255679 to your computer and use it in GitHub Desktop.
python: How to Zig-Zag a Toolpath Array
"""Demonstration of how to 'zig-zag' an m x n x 3
python array (m = passes, n = points per pass).
Includes some array creation/mod tricks
>>> import numpy as np
>>> a = np.random.randn(4,3,3)
>>> a
array([[[-0.74865489, 0.533361 , 0.51997179],
[-0.72109915, -0.74988902, -2.61720425],
[-0.72792437, 2.43027423, 0.84132144]],
[[-1.82027469, -0.2597997 , -2.01250823],
[ 0.37021219, -0.89890801, -0.75788127],
[-0.77095731, -1.01914784, -0.50027295]],
[[-0.96289404, -0.48019978, 0.18274626],
[-0.49190084, -0.14846301, -0.41655213],
[ 1.99714785, 0.17308239, -0.36529016]],
[[-1.65683619, -0.25826898, -0.51135952],
[ 0.83005551, 1.26310339, -1.79057156],
[-0.62770164, -1.2752175 , -0.25720492]]])
>>> a[:,:,2] = np.c_[[[0, 1, 2]] * 4]
>>> a
array([[[-0.74865489, 0.533361 , 0. ],
[-0.72109915, -0.74988902, 1. ],
[-0.72792437, 2.43027423, 2. ]],
[[-1.82027469, -0.2597997 , 0. ],
[ 0.37021219, -0.89890801, 1. ],
[-0.77095731, -1.01914784, 2. ]],
[[-0.96289404, -0.48019978, 0. ],
[-0.49190084, -0.14846301, 1. ],
[ 1.99714785, 0.17308239, 2. ]],
[[-1.65683619, -0.25826898, 0. ],
[ 0.83005551, 1.26310339, 1. ],
[-0.62770164, -1.2752175 , 2. ]]])
>>> a[1::2] = a[1::2, -1::-1]
>>> a
array([[[-0.74865489, 0.533361 , 0. ],
[-0.72109915, -0.74988902, 1. ],
[-0.72792437, 2.43027423, 2. ]],
[[-0.77095731, -1.01914784, 2. ],
[ 0.37021219, -0.89890801, 1. ],
[-1.82027469, -0.2597997 , 0. ]],
[[-0.96289404, -0.48019978, 0. ],
[-0.49190084, -0.14846301, 1. ],
[ 1.99714785, 0.17308239, 2. ]],
[[-0.62770164, -1.2752175 , 2. ],
[ 0.83005551, 1.26310339, 1. ],
[-1.65683619, -0.25826898, 0. ]]])
"""
@subnivean
Copy link
Author

Captured output from an IPython Session

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment