Skip to content

Instantly share code, notes, and snippets.

@ixxra
Created June 20, 2013 15:52
Show Gist options
  • Save ixxra/5824013 to your computer and use it in GitHub Desktop.
Save ixxra/5824013 to your computer and use it in GitHub Desktop.
sagenb.org has made worksheets private, this is part of my notebook on cantor dust
#This goes in one cell alone
class Carpet:
'''
This class contains the generator for Carpet fractals generated by regular polygons.
It generates the carpet vertices recursively, which is not very efficient, but it
goes along with the mathematical definition.
'''
def __init__(self, x, y, sides, r, contraction):
'''
x, y: Top of the carpet
sides: number of sides
r: this parameter is related to the length of the first polygon side
contraction: Factor of the contraction applied on each step.
'''
angle = 2*pi/sides
self.x = x
self.y = y
self.r = r
self.factor = 1 - contraction
self.contraction = contraction
self.polar = [(cos(n*angle), sin(n*angle)) for n in range(sides)]
def _children(self, x, y, radius, step=0):
if step==0:
yield (x, y)
else:
newRadius = self.contraction*radius
deltaR = self.factor*radius
for dX, dY in self.polar:
for f in self._children(x + deltaR*dX, y + deltaR*dY, newRadius, step - 1):
yield f
def children(self, steps=0):
return self._children(self.x, self.y, self.r, steps)
#This piece of code goes in another cell
sides = 5
contraction = .3
r = 500
c = Carpet(0, 0, sides, r, contraction)
#Copy this in another cell
#Beware, with the above configuration, for more than 6 steps the calculation is very lenghty
#Also, not that cantor dust is perfect (it should look sparse)
#but the point size makes it appear clustered
points = list(c.children(steps=4))
list_plot(points)
#And finally, one more cell
#This is more appropiated for dust
list_plot(points, pointsize=2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment