返回介绍

绘制 L-System 的分形图

发布于 2025-02-25 22:46:24 字数 2456 浏览 0 评论 0 收藏 0

相关文档:L-System 分形

# -*- coding: utf-8 -*-
#L-System(Lindenmayer system) 是一种用字符串替代产生分形图形的算法
from math import sin, cos, pi
import matplotlib.pyplot as pl
from matplotlib import collections

class L_System(object):
  def __init__(self, rule):
    info = rule['S']
    for i in range(rule['iter']):
      ninfo = []
      for c in info:
        if c in rule:
          ninfo.append(rule[c])
        else:
          ninfo.append(c)
      info = "".join(ninfo)
    self.rule = rule
    self.info = info

  def get_lines(self):
    d = self.rule['direct']
    a = self.rule['angle']
    p = (0.0, 0.0)
    l = 1.0
    lines = []
    stack = []
    for c in self.info:
      if c in "Ff":
        r = d * pi / 180
        t = p[0] + l*cos(r), p[1] + l*sin(r)
        lines.append(((p[0], p[1]), (t[0], t[1])))
        p = t
      elif c == "+":
        d += a
      elif c == "-":
        d -= a
      elif c == "[":
        stack.append((p,d))
      elif c == "]":
        p, d = stack[-1]
        del stack[-1]
    return lines

rules = [
  {
    "F":"F+F--F+F", "S":"F",
    "direct":180,
    "angle":60,
    "iter":5,
    "title":"Koch"
  },
  {
    "X":"X+YF+", "Y":"-FX-Y", "S":"FX",
    "direct":0,
    "angle":90,
    "iter":13,
    "title":"Dragon"
  },
  {
    "f":"F-f-F", "F":"f+F+f", "S":"f",
    "direct":0,
    "angle":60,
    "iter":7,
    "title":"Triangle"
  },
  {
    "X":"F-[[X]+X]+F[+FX]-X", "F":"FF", "S":"X",
    "direct":-45,
    "angle":25,
    "iter":6,
    "title":"Plant"
  },
  {
    "S":"X", "X":"-YF+XFX+FY-", "Y":"+XF-YFY-FX+",
    "direct":0,
    "angle":90,
    "iter":6,
    "title":"Hilbert"
  },
  {
    "S":"L--F--L--F", "L":"+R-F-R+", "R":"-L+F+L-",
    "direct":0,
    "angle":45,
    "iter":10,
    "title":"Sierpinski"
  },

]

def draw(ax, rule, iter=None):
  if iter!=None:
    rule["iter"] = iter
  lines = L_System(rule).get_lines()
  linecollections = collections.LineCollection(lines)
  ax.add_collection(linecollections, autolim=True)
  ax.axis("equal")
  ax.set_axis_off()
  ax.set_xlim(ax.dataLim.xmin, ax.dataLim.xmax)
  ax.invert_yaxis()

fig = pl.figure(figsize=(7,4.5))
fig.patch.set_facecolor("w")

for i in xrange(6):
  ax = fig.add_subplot(231+i)
  draw(ax, rules[i])

fig.subplots_adjust(left=0,right=1,bottom=0,top=1,wspace=0,hspace=0)
pl.show()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文