Skip to content

Instantly share code, notes, and snippets.

@jbn
Created May 22, 2016 15:59
Show Gist options
  • Save jbn/bc7e3f67a3ee1a0f1e6d5d827c64265f to your computer and use it in GitHub Desktop.
Save jbn/bc7e3f67a3ee1a0f1e6d5d827c64265f to your computer and use it in GitHub Desktop.
Silly little step spiral in Python using itertools.
import itertools
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
def step_spiral(x_0=0, y_0=0):
point = (0, x_0, y_0)
yield point
triplets = zip(itertools.count(1),
itertools.cycle([0, 0, 1, 0, 0, -1]),
itertools.cycle([1, 0, 0, -1, 0, 0]))
for (i, x, y) in triplets:
point = (i, point[1] + x*i**2, point[2] + y*i**2)
yield point
points = list(itertools.takewhile(lambda trip: trip[0] < 100, step_spiral()))
points = np.array(points)
plt.plot(points[:,1], points[:,2]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment