Advertisement
lewapkon

Sierpinski's carpet

Feb 2nd, 2014
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.94 KB | None | 0 0
  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3. import numpy as np
  4. from matplotlib import pyplot as plt
  5.  
  6. n = 5
  7. m = 3**n
  8. carpet = np.ones((m, m), dtype=bool) # create two-dimensional bitarray
  9.  
  10. def sierpinski(a, b, l, n): # a,b - offset; l - length
  11.     if n == 0:
  12.         return
  13.     l /= 3 # offset of centre
  14.     for x in xrange(l+a, l+l+a):
  15.         for y in xrange(l+b, l+l+b):
  16.             carpet[x][y] = False
  17.     sierpinski(a+0,   b+0,   l, n-1)
  18.     sierpinski(a+0,   b+l,   l, n-1)
  19.     sierpinski(a+0,   b+l+l, l, n-1)
  20.  
  21.     sierpinski(a+l,   b+0,   l, n-1)
  22.     sierpinski(a+l,   b+l+l, l, n-1)
  23.  
  24.     sierpinski(a+l+l, b+0,   l, n-1)
  25.     sierpinski(a+l+l, b+l,   l, n-1)
  26.     sierpinski(a+l+l, b+l+l, l, n-1)
  27.  
  28.  
  29. sierpinski(0, 0, m, n)
  30.  
  31. plt.imshow(carpet, interpolation='nearest')
  32. """ hide axes and show the sierpinski's carpet """
  33. plt.gca().axes.get_xaxis().set_visible(False)
  34. plt.gca().axes.get_yaxis().set_visible(False)
  35. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement