Skip to content

Instantly share code, notes, and snippets.

@Devligue
Last active March 2, 2017 12:19
Show Gist options
  • Save Devligue/7994429e313c6fe94e118d0063548f91 to your computer and use it in GitHub Desktop.
Save Devligue/7994429e313c6fe94e118d0063548f91 to your computer and use it in GitHub Desktop.
"Fractal Spirograph" with Processing.py. The visuals are inspired by images and explanation on C. J. Chen's blog: http://benice-equation.blogspot.ca/2012/01/fractal-spirograph.html This code is a python version of the original one presented in video: https://www.youtube.com/watch?v=0dwJ-bkJwDI
resolution = 10
k = 4
from orbit import Orbit
from config import resolution
path = []
def setup():
size(600, 600)
global sun
sun = Orbit(300, 300, 140, 0)
next = sun
for i in range(10):
next = next.add_child()
global last
last = next
def draw():
global path
background(51)
noFill()
for i in range(resolution):
next = sun
while next:
next.update()
next.show()
next = next.child
path.append(PVector(last.x, last.y))
beginShape()
stroke(255, 0, 255)
strokeWeight(1)
for pos in path:
vertex(pos.x, pos.y)
endShape()
from config import resolution, k
class Orbit():
def __init__(self, x, y, r, n, parent=None):
global resolution
self.x = x
self.y = y
self.r = r
self.n = n
self.parent = parent
self.child = None
self.speed = radians(pow(-k, n-1))/resolution
self.angle = -PI/2
def add_child(self):
newr = self.r/2.9
newx = self.x + self.r + newr
newy = self.y
self.child = Orbit(newx, newy, newr, self.n + 1, self)
return self.child
def update(self):
if self.parent:
self.angle += self.speed
rsum = self.r + self.parent.r
self.x = self.parent.x + rsum * cos(self.angle)
self.y = self.parent.y + rsum * sin(self.angle)
# ellipse(x2, y2, r2*2, r2*2)
def show(self):
stroke(255, 100)
strokeWeight(1)
noFill()
ellipse(self.x, self.y, self.r*2, self.r*2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment