Skip to content

Commit

Permalink
12
Browse files Browse the repository at this point in the history
  • Loading branch information
villares committed Dec 13, 2024
1 parent 97113e2 commit cfbc587
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 0 deletions.
Binary file added 2024/sketch_2024_12_12/sketch_2024_12_12.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
109 changes: 109 additions & 0 deletions 2024/sketch_2024_12_12/sketch_2024_12_12.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
from itertools import permutations
import py5
import numpy as np

N = 4 # number of colors

def setup():
global grids
py5.size(600, 600)
py5.color_mode(py5.HSB)
Grid.CS = 65
Grid.colors = [py5.color((12 + (255 / N) * i) , 200 , 200) for i in range(4)]
grids = [Grid(shape=(3, 3, 4))]
for _ in range(3):
grids.append(grids[-1].rotated90())
#grids.append(grids[-1].alternate())

def draw():
py5.background(200)
py5.translate(Grid.CS, Grid.CS)
x = y = 0
#for g in set(grids): # to test duplicate removal
for g in [grids[1], grids[0]] + grids[2:]:
with py5.push_matrix():
py5.translate(x, y)
g.draw()
x += Grid.CS * (g.shape[1] + 1)
if x > py5.width - Grid.CS * 2:
x = 0
y += Grid.CS * (g.shape[0] + 1)


class Grid:

colors = [0]
CS = 40 # Cell size

def __init__(self, elements=None, shape=(4, 4, 4)):
self.shape = shape
color_indices = range(len(self.colors))
if elements is None:
elements = py5.random_sample(color_indices, shape[0] * shape[1] * shape[2])
self.array = np.array(elements).reshape(shape)

def rotated90(self):
i, j, k = self.shape
return Grid(self.rot90_and_roll(self.array), shape=(j, i, k))

def alternate(self):
return Grid((self.array + 1) % len(self.colors), shape=self.shape)

def roll(self):
self.array = np.roll(self.array, 1, axis=2)

@staticmethod
def rot90_and_roll(a):
"""
This will have more stuff later...
... it will need to roll the sub-elements
"""
return np.roll(np.rot90(a, 1), -1, axis=2)

def __eq__(self, other):
return hash(self) == hash(other)

def __hash__(self):
"""
Makes rotations and different color alternatives equivalent.
"""
a = self.array
h = hash(a.tobytes())
values, inverse_indices = np.unique(a, return_inverse=True)
for vs in permutations(values):
a = np.array(vs)[inverse_indices].reshape(self.shape)
h = min(h, hash(a.tobytes()))
for _ in range(3):
a = self.rot90_and_roll(a)
h = min(h, hash(a.tobytes()))
return h

def draw(self):
rows, cols, subelements = self.array.shape
CS = self.CS
for r in range(rows):
y = self.CS * r
for c in range(cols):
x = CS * c
tris = np.array((
((0, 0), (1, 0), (0.5, 0.5)),
((1, 0), (1, 1), (0.5, 0.5)),
((1, 1), (0, 1), (0.5, 0.5)),
((0, 1), (0, 0), (0.5, 0.5)),
)) * CS + [x, y]
for s in range(subelements):
py5.stroke_weight(0.1)
py5.fill(self.colors[self.array[r, c, s]])
py5.stroke(self.colors[self.array[r, c, s]])
#py5.triangle(*tris[s][0], *tris[s][1], *tris[s][2])
with py5.begin_closed_shape():
py5.vertices(tris[s])

def key_pressed():
if py5.key == 's':
py5.save_frame('###.png')
elif py5.key == ' ':
for g in grids:
g.roll()

py5.run_sketch(block=False)
10 changes: 10 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,16 @@ Here are listed some of the tools I have been using more recently:
2024 \| [<b>2023</b>](2023.md) \| [<b>2022</b>](2022.md) \| [<b>2021</b>](2021.md) \| [<b>2020</b>](2020.md) \| [<b>2019</b>](2019.md) \| [<b>2018</b>](2018.md)


---

### sketch_2024_12_12

![sketch_2024_12_12](https://raw.githubusercontent.com/villares/sketch-a-day/main/2024/sketch_2024_12_12/sketch_2024_12_12.png)

[sketch_2024_12_12](https://github.com/villares/sketch-a-day/tree/main/2024/sketch_2024_12_12) [[py5](https://py5coding.org/)]

Maybe, just maybe, I'm getting to enjoy using #NumPy.

---

### sketch_2024_12_11
Expand Down

0 comments on commit cfbc587

Please sign in to comment.