Created
March 20, 2022 20:51
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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