Skip to content

Instantly share code, notes, and snippets.

@diegogangl
Last active June 29, 2017 14:04
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 diegogangl/19d71ce279621eea0d5d42bf74d6afbf to your computer and use it in GitHub Desktop.
Save diegogangl/19d71ce279621eea0d5d42bf74d6afbf to your computer and use it in GitHub Desktop.
A simple function to get indices around an index "i" in a diamond shape. Returns a list of indices usable for a 2D grid in a flat list.
def get_indices_diamond(i, radius, side):
""" Get indices around i in a diamond shape """
indices = []
indices += [i + j for j in range(radius + 1)]
indices += [i - j for j in range(1, radius + 1)]
for step in range(1, radius):
indices += [i + side * step + j for j in range(radius - (step - 1))]
indices += [i + side * step - j for j in range(radius - (step - 1))]
indices += [i-side * step + j for j in range(radius - (step - 1))]
indices += [i-side * step - j for j in range(radius - (step - 1))]
return [int(index) for index in indices]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment