Skip to content

Instantly share code, notes, and snippets.

@beotiger
Last active April 6, 2024 12:59
Show Gist options
  • Select an option

  • Save beotiger/0e5ad4eeafa6963fea8cf537d99d6a18 to your computer and use it in GitHub Desktop.

Select an option

Save beotiger/0e5ad4eeafa6963fea8cf537d99d6a18 to your computer and use it in GitHub Desktop.
Mondrian-Inspired Generative Art in Python using sparse matrix
class Block:
def __init__(self, c, main):
self.c = c
self.main = main
self.colRange = self.randomLengthGen(main.cols + 1)
self.rowRange = self.randomLengthGen(main.rows + 1)
self.block = dict(
((i, j), Cell(i * main.wx, j * main.hx, main.wx, main.hx, j)) for i in range(self.colRange[0], self.colRange[1]) for j in range(self.rowRange[0], self.rowRange[1])
)
def display(self):
# strokeWeight(3);
for i in range(self.colRange[0], self.colRange[1]):
for j in range(self.rowRange[0], self.rowRange[1]):
color = 'black' if i == self.colRange[0] or i == self.colRange[1] - 1 else self.c
self.block[i, j].displayCell(color, self.main) # type: ignore
def randomLengthGen(self, length):
while True:
if length < 6: return (1, 5)
a = random.randint(0, length)
b = random.randint(0, length)
if abs(a - b) >= 4: break
return (min(a, b), max(a, b))
class Cell:
def __init__(self, x, y, w, h, row):
self.x = x
self.y = y
self.w = w
self.h = h
self.row = row
def displayCell(self, color, main):
# Main: .drawLine(self, color, x1, y1, x2, y2, **kwargs):
if self.row % 2 == 0:
main.drawLine(color, self.x, self.y, self.x + self.w, self.y + self.h)
else:
main.drawLine(color, self.x + self.w, self.y, self.x, self.y + self.h)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment