Skip to content

Instantly share code, notes, and snippets.

@whatalnk
Last active March 4, 2016 13:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save whatalnk/0e7a0994958cad5a1e94 to your computer and use it in GitHub Desktop.
Save whatalnk/0e7a0994958cad5a1e94 to your computer and use it in GitHub Desktop.
Fractal: Minkowski sausage
class Point:
thn = atan(0.5)
thnn = 3 * PI / 2
def __init__(self, x, y):
self.x = x
self.y = y
def nextPoints(self, p1):
pnx = ((p1.x - self.x) * cos(self.thn) - (p1.y - self.y) * sin(self.thn)) / sqrt(5) + self.x
pny = ((p1.x - self.x) * sin(self.thn) + (p1.y - self.y) * cos(self.thn)) / sqrt(5) + self.y
pn = Point(pnx, pny)
pnnx = ((pn.x - self.x) * cos(self.thnn) - (pn.y - self.y) * sin(self.thnn)) + pn.x
pnny = ((pn.x - self.x) * sin(self.thnn) + (pn.y - self.y) * cos(self.thnn)) + pn.y
pnn = Point(pnnx, pnny)
return([pn, pnn])
curr = [Point(0, 0), Point(200, 0), Point(200, 200), Point(0, 200)]
nex = list()
n = 0
def setup():
size(500, 500)
noLoop()
fill(0)
textSize(16)
def draw():
global curr, nex, n
if n > 5:
exit()
background(250, 250, 250)
text("n = {}".format(n), 25, 25)
translate(width / 2 - 100, height / 2 - 100)
for i in range(len(curr)):
if i == len(curr) - 1:
p0 = curr[i]
p1 = curr[0]
line(p0.x, p0.y, p1.x, p1.y)
else:
p0 = curr[i]
p1 = curr[i+1]
line(p0.x, p0.y, p1.x, p1.y)
# saveFrame("minkowski-sausage-{}.png".format(n))
for i in range(len(curr)):
if i == len(curr) - 1:
p0 = curr[i]
p1 = curr[0]
nex.append(p0)
nex += p0.nextPoints(p1)
else:
p0 = curr[i]
p1 = curr[i+1]
nex.append(p0)
nex += p0.nextPoints(p1)
curr = nex[:]
nex = list()
n += 1
noLoop()
def mousePressed():
loop()
# mouseReleased() broken?
# def mouseReleased():
# noLoop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment