Created
February 22, 2019 15:58
-
-
Save typesupply/d1f8c7eed0563567370162cfc956256c to your computer and use it in GitHub Desktop.
This file contains 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
import time | |
from random import shuffle | |
# ---------- | |
# The Inputs | |
# ---------- | |
pageSize = 500 | |
maximumFrameCalculationDuration = 0.1 | |
minimumPixelSize = 5 | |
glyph = BezierPath() | |
glyph.textBox( | |
"Q", | |
(0, 0, pageSize, pageSize), | |
font="QueueMono-Bold", | |
fontSize=pageSize * 0.9, | |
align="center" | |
) | |
# -------- | |
# The Guts | |
# -------- | |
def rasterize(): | |
deadPixels = [] | |
livePixels = [ | |
dict( | |
black=True, | |
size=pageSize, | |
pos=(0, 0) | |
) | |
] | |
finalPixels = [] | |
splits = 2 | |
subdivisionCount = splits * splits | |
start = time.time() | |
def keepGoing(): | |
return time.time() - start < maximumFrameCalculationDuration | |
while livePixels: | |
if not keepGoing(): | |
break | |
shuffle(livePixels) | |
previous = livePixels[0] | |
x, y = previous["pos"] | |
size = previous["size"] / splits | |
half = size / 2 | |
subsectionPositions = [] | |
for xD in range(splits): | |
testX = x + (xD * size) | |
for yD in range(splits): | |
testY = y + (yD * size) | |
subsectionPositions.append((testX, testY)) | |
subsections = [] | |
for x, y in subsectionPositions: | |
if not keepGoing(): | |
break | |
subsections.append( | |
dict( | |
black=glyph.pointInside((x + half, y + half)), | |
size=size, | |
pos=(x, y) | |
) | |
) | |
testedAllSubsections = len(subsections) == subdivisionCount | |
if testedAllSubsections: | |
del livePixels[0] | |
deadPixels.append(previous) | |
if size / splits < minimumPixelSize: | |
finalPixels += subsections | |
else: | |
livePixels += subsections | |
return deadPixels, finalPixels + livePixels | |
# ------------ | |
# Make A Movie | |
# ------------ | |
for i in range(100): | |
newPage(pageSize, pageSize) | |
frameDuration(0.1) | |
fill(1, 1, 1) | |
rect(0, 0, pageSize, pageSize) | |
deadPixels, finalPixels = rasterize() | |
border = 0.5 | |
for pixel in deadPixels: | |
if pixel["black"]: | |
fill(1, 0, 0, 0.1) | |
else: | |
fill(1, 1, 0, 0.1) | |
x, y = pixel["pos"] | |
s = pixel["size"] | |
x += border | |
y += border | |
s -= border * 2 | |
oval(x, y, s, s) | |
fill(1, 0, 0, 0.6) | |
for pixel in finalPixels: | |
if pixel["black"]: | |
x, y = pixel["pos"] | |
s = pixel["size"] | |
x += border | |
y += border | |
s -= border * 2 | |
oval(x, y, s, s) | |
saveImage("~/Desktop/random_raster_letter.gif") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment