image.png

    1. from math import sin, cos, pi
    2. import matplotlib.pyplot as pl
    3. from matplotlib import collections
    4. class L_System(object):
    5. def __init__(self, rule):
    6. info = rule['S']
    7. for i in range(rule['iter']):
    8. ninfo = []
    9. for c in info:
    10. if c in rule:
    11. ninfo.append(rule[c])
    12. else:
    13. ninfo.append(c)
    14. info = "".join(ninfo)
    15. self.rule = rule
    16. self.info = info
    17. def get_lines(self):
    18. d = self.rule['direct']
    19. a = self.rule['angle']
    20. p = (0.0, 0.0)
    21. l = 1.0
    22. lines = []
    23. stack = []
    24. for c in self.info:
    25. if c in "Ff":
    26. r = d * pi / 180
    27. t = p[0] + l * cos(r), p[1] + l * sin(r)
    28. lines.append(((p[0], p[1]), (t[0], t[1])))
    29. p = t
    30. elif c == "+":
    31. d += a
    32. elif c == "-":
    33. d -= a
    34. elif c == "[":
    35. stack.append((p, d))
    36. elif c == "]":
    37. p, d = stack[-1]
    38. del stack[-1]
    39. return lines
    40. rules = [
    41. {
    42. "F": "F+F--F+F", "S": "F",
    43. "direct": 180,
    44. "angle": 60,
    45. "iter": 8,
    46. "title": "Koch"
    47. },
    48. {
    49. "X": "X+YF+", "Y": "-FX-Y", "S": "FX",
    50. "direct": 0,
    51. "angle": 90,
    52. "iter": 13,
    53. "title": "Dragon"
    54. },
    55. {
    56. "f": "F-f-F", "F": "f+F+f", "S": "f",
    57. "direct": 0,
    58. "angle": 60,
    59. "iter": 7,
    60. "title": "Triangle"
    61. },
    62. {
    63. "X": "F-[[X]+X]+F[+FX]-X", "F": "FF", "S": "X",
    64. "direct": -45,
    65. "angle": 25,
    66. "iter": 8,
    67. "title": "Plant"
    68. },
    69. {
    70. "S": "X", "X": "-YF+XFX+FY-", "Y": "+XF-YFY-FX+",
    71. "direct": 0,
    72. "angle": 90,
    73. "iter": 6,
    74. "title": "Hilbert"
    75. },
    76. {
    77. "S": "L--F--L--F", "L": "+R-F-R+", "R": "-L+F+L-",
    78. "direct": 0,
    79. "angle": 45,
    80. "iter": 10,
    81. "title": "Sierpinski"
    82. },
    83. ]
    84. def draw(ax, rule, iter=None):
    85. if iter != None:
    86. rule["iter"] = iter
    87. lines = L_System(rule).get_lines()
    88. linecollections = collections.LineCollection(lines)
    89. ax.add_collection(linecollections, autolim=True)
    90. ax.axis("equal")
    91. ax.set_axis_off()
    92. ax.set_xlim(ax.dataLim.xmin, ax.dataLim.xmax)
    93. ax.invert_yaxis()
    94. fig = pl.figure(figsize=(7, 4.5))
    95. fig.patch.set_facecolor("papayawhip")
    96. for i in range(6):
    97. ax = fig.add_subplot(231 + i)
    98. draw(ax, rules[i])
    99. help(ax.add_collection)
    100. fig.subplots_adjust(left=0, right=1, bottom=0, top=1, wspace=0, hspace=0)
    101. pl.show()

    image.png

    1. import numpy as np
    2. import pylab as pl
    3. from matplotlib import cm
    4. def iter_point(c):
    5. z = c
    6. for i in range(1, 100): # 最多迭代100次
    7. if abs(z) > 2:
    8. break # 半径大于2则认为逃逸
    9. z = z * z + c
    10. return i # 返回迭代次数
    11. def draw_mandelbrot(cx, cy, d):
    12. """ 绘制点(cx, cy)附近正负d的范围的Mandelbrot """
    13. x0, x1, y0, y1 = cx - d, cx + d, cy - d, cy + d
    14. y, x = np.ogrid[y0:y1:200j, x0:x1:200j]
    15. c = x + y * 1j
    16. mandelbrot = np.frompyfunc(iter_point, 1, 1)(c).astype(np.float)
    17. pl.imshow(mandelbrot, cmap=cm.jet, extent=[x0, x1, y0, y1])
    18. pl.gca().set_axis_off()
    19. x, y = 0.27322626, 0.595153338
    20. pl.subplot(231)
    21. draw_mandelbrot(-0.5, 0, 1.5)
    22. for i in range(2, 7):
    23. pl.subplot(230 + i)
    24. draw_mandelbrot(x, y, 0.2 ** (i - 1))
    25. pl.subplots_adjust(0.02, 0, 0.98, 1, 0.02, 0)
    26. pl.show()