Skip to content

Instantly share code, notes, and snippets.

@jsundram
Last active April 1, 2016 22:54
Show Gist options
  • Save jsundram/04ed2a582bbb9c96683364c8d5a64f58 to your computer and use it in GitHub Desktop.
Save jsundram/04ed2a582bbb9c96683364c8d5a64f58 to your computer and use it in GitHub Desktop.
fractals in processing.py
import hashlib
import os
import shutil
import time
import utils
seed = int(time.time())
timestamp = utils.encode(hex(seed))
run_id = None
start_level = int(random(100))
movie, cycle_length = False, 360
def setup():
global run_id
size(800, 800, P2D)
blendMode(SUBTRACT)
rectMode(CENTER) # easily switch between ellipse and rect
smooth(4)
randomSeed(int(time.time()))
#if not movie: noLoop()
codefile = os.path.realpath(__file__)
with open(codefile, 'rb') as f:
fingerprint = utils.encode(hashlib.md5(f.read()).hexdigest())
run_id = '%s-%s' % (fingerprint, timestamp)
shutil.copy(codefile, 'code-%s.pyde' % run_id)
def draw():
background(255)
fractal(.5*width, .5*height, .5*height, angle=radians(270), level=start_level)
if movie:
print(frameCount)
saveFrame(os.path.join(run_id, '####.png')) # stitch together with ffmpeg post-run
if cycle_length <= frameCount:
exit()
if frameCount == 1: # Save the first frame of every run
saveFrame('sketch-%s.png' % run_id)
def triangle(x, y, r, angle, l):
beginShape()
a = angle + l*(frameCount - 1) * (TWO_PI/360/20)
vertex(x + r * cos(a), y + r * sin(a))
vertex(x + r * cos(a + TWO_PI/3), y + r * sin(a + TWO_PI/3))
vertex(x + r * cos(a + 4 * PI/3), y + r * sin(a + 4 * PI/3))
endShape(CLOSE)
def fractal(x, y, radius, angle=0, level=0):
noStroke()
a = 255
colors = map(lambda c: color(*c), [(255, 0, 0, a), (0, 255, 0, a), (0, 0, 255, a)])
fill(colors[level % len(colors)])
pushMatrix()
translate(x, y); x, y = 0, 0
triangle(x, y, radius, angle, level)
if True and 2 < radius:
nr = .5 * radius
yoff = nr # height*.125*cos(PI*noise(y))
fractal(x + nr, y + yoff, radius / 2, angle - radians(60), level + 1)
fractal(x - nr, y + yoff, radius / 2, angle + radians(300), level + 1)
popMatrix()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment