Skip to content

Instantly share code, notes, and snippets.

@Ge0rg3
Created March 20, 2022 20:51
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 Ge0rg3/282dd5671d755acbf13352a7ae8e2d5e to your computer and use it in GitHub Desktop.
Save Ge0rg3/282dd5671d755acbf13352a7ae8e2d5e to your computer and use it in GitHub Desktop.
Reference code for iterating through a 2d array (not literally, in this sense) via either Row/Col, and optionally in a zigzag pattern.
pixels_array = [
0, 1, 2, 3,
4, 5, 6, 7,
8, 9, 10, 11
]
width, height = (4, 3)
order = "ROW"
zigzag = True
zigzag_start = "RIGHT"
start_a = 0
start_b = 0
step_a = 1
step_b = 1
if order == "ROW":
end_a = height
end_b = width
else:
end_a = width
end_b = height
for a in range(start_a, end_a, step_a):
if zigzag:
if zigzag_start == "RIGHT":
zigzag_flip = 0
elif zigzag_start == "LEFT":
zigzag_flip = 1
if a % 2 == zigzag_flip:
start_b = end_b - 1
end_b = -1
step_b = -1
else:
start_b = 0
step_b = 1
if order == "ROW":
end_b = width
else:
end_b = height
for b in range(start_b, end_b, step_b):
if order == "ROW":
index = (a * width) + b
else:
index = (b * width) + a
print(pixels_array[index])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment