Created
November 28, 2013 00:32
-
-
Save ejetzer/7685490 to your computer and use it in GitHub Desktop.
Dragon Curve
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
from scene import * | |
from math import atan, cos, sin, sqrt, log | |
ratio = 0.5 | |
def bump(start, end): | |
x1, y1 = start | |
x2, y2 = end | |
length = sqrt((x2-x1)**2 + (y2-y1)**2) | |
midx = (x2 + x1) / 2 | |
midy = (y2 + y1) / 2 | |
normx = (y1 - y2) * ratio | |
normy = (x2 - x1) * ratio | |
x = midx + normx | |
y = midy + normy | |
return x, y | |
def iteration(pts, *tpts): | |
pts = tuple(pts) + tpts | |
iterated = [] | |
for i, pt in enumerate(pts[:-1]): | |
iterated.append(pt) | |
nxt = pts[i+1] | |
new = bump(pt, nxt) if i % 2 else bump(nxt, pt) | |
iterated.append(new) | |
iterated.append(pts[-1]) | |
return iterated | |
def dragon(pts, lim=3): | |
for i in range(lim): | |
pts = iteration(pts) | |
return pts | |
class MyScene (Scene): | |
def setup(self): | |
# This will be called before the first frame is drawn. | |
background(1, 1, 1) | |
stroke(0, 0, 0) | |
stroke_weight(1) | |
x1, y1 = 300, 400 | |
x2, y2 = 800, 400 | |
line(x1, y1, x2, y2) | |
self.pts = ((x1, y1), (x2, y2)) | |
self.count = 0 | |
def draw(self): | |
# This will be called for every frame (typically 60 times per second). | |
pass | |
def touch_began(self, touch): | |
background(1, 1, 1) | |
self.pts = iteration(self.pts) | |
for i, pt in enumerate(self.pts[:-1]): | |
x1, y1 = pt | |
x2, y2 = self.pts[i+1] | |
line(x1, y1, x2, y2) | |
def touch_moved(self, touch): | |
pass | |
def touch_ended(self, touch): | |
pass | |
run(MyScene()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment