Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- # -*- coding: utf-8 -*-
- import numpy as np
- from matplotlib import pyplot as plt
- n = 5
- m = 3**n
- carpet = np.ones((m, m), dtype=bool) # create two-dimensional bitarray
- def sierpinski(a, b, l, n): # a,b - offset; l - length
- if n == 0:
- return
- l /= 3 # offset of centre
- for x in xrange(l+a, l+l+a):
- for y in xrange(l+b, l+l+b):
- carpet[x][y] = False
- sierpinski(a+0, b+0, l, n-1)
- sierpinski(a+0, b+l, l, n-1)
- sierpinski(a+0, b+l+l, l, n-1)
- sierpinski(a+l, b+0, l, n-1)
- sierpinski(a+l, b+l+l, l, n-1)
- sierpinski(a+l+l, b+0, l, n-1)
- sierpinski(a+l+l, b+l, l, n-1)
- sierpinski(a+l+l, b+l+l, l, n-1)
- sierpinski(0, 0, m, n)
- plt.imshow(carpet, interpolation='nearest')
- """ hide axes and show the sierpinski's carpet """
- plt.gca().axes.get_xaxis().set_visible(False)
- plt.gca().axes.get_yaxis().set_visible(False)
- plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement