fork download
  1. # your code goes here
  2. #0JLQsNGB0LjQu9C10L3QutC+INCQ0YDRgtC10Lwg
  3. import matplotlib.pyplot as plt
  4. import numpy as np
  5. from matplotlib.patches import Polygon
  6.  
  7. def _get_midpoints(vertices):
  8. mid1 = (vertices[0] + vertices[1]) / 2
  9. mid2 = (vertices[1] + vertices[2]) / 2
  10. mid3 = (vertices[0] + vertices[2]) / 2
  11. return mid1, mid2, mid3
  12.  
  13. def _draw_sierpinski_recursive(ax, vertices, depth, color):
  14. if depth == 0:
  15. triangle = Polygon(vertices, facecolor=color, edgecolor='none')
  16. ax.add_patch(triangle)
  17. else:
  18. v1, v2, v3 = vertices
  19. mid1, mid2, mid3 = _get_midpoints(vertices)
  20. _draw_sierpinski_recursive(ax, np.array([v1, mid1, mid3]), depth - 1, color)
  21. _draw_sierpinski_recursive(ax, np.array([mid1, v2, mid2]), depth - 1, color)
  22. _draw_sierpinski_recursive(ax, np.array([mid3, mid2, v3]), depth - 1, color)
  23.  
  24. def plot_sierpinski(depth, color='black', figsize=(10, 8.66), title_on=True):
  25. fig, ax = plt.subplots(figsize=figsize)
  26. height = np.sqrt(3) / 2
  27. initial_vertices = np.array([
  28. [0, 0],
  29. [1, 0],
  30. [0.5, height]
  31. ])
  32. _draw_sierpinski_recursive(ax, initial_vertices, depth, color)
  33. ax.set_aspect('equal')
  34. ax.axis('off')
  35. if title_on:
  36. plt.title(f"ТрикутникСерпінського(Глибина){depth}) ", fontsize=16)
  37. plt.show()
  38.  
  39. #plot_sierpinski(depth=6, color='#004C99')
  40. plot_sierpinski(depth=3, color='#004C99', title_on=False)
  41.  
  42.  
Success #stdin #stdout #stderr 4.84s 69364KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Fontconfig error: No writable cache directories