Python如何生成树形图案


Posted in Python onJanuary 03, 2018

本文实例为大家分享了Python生成树形图案的具体代码,供大家参考,具体内容如下

先看一下效果,见下图。

Python如何生成树形图案

上面这颗大树是使用Python + Tkinter绘制的,主要原理为使用分形画树干、树枝,最终叶节点上画上绿色圆圈代表树叶。当然,为了看起来更真实,绘制过程中也加入了一些随机变化,比如树枝会稍微有些扭曲而不是一条直线,分叉的角度、长短等都会随机地作一些偏移等。

以下是完整源代码:

# -*- coding: utf-8 -*- 
 
import Tkinter 
import sys, random, math 
 
class Point(object): 
  def __init__(self, x, y): 
    self.x = x 
    self.y = y 
 
  def __str__(self): 
    return "<Point>: (%f, %f)" % (self.x, self.y) 
 
class Branch(object): 
  def __init__(self, bottom, top, branches, level = 0): 
    self.bottom = bottom 
    self.top = top 
    self.level = level 
    self.branches = branches 
    self.children = [] 
 
  def __str__(self): 
    s = "Top: %s, Bottom: %s, Children Count: %d" % / 
      (self.top, self.bottom, len(self.children)) 
    return s 
 
  def nextGen(self, n = -1, rnd = 1): 
    if n <= 0: n = self.branches 
    if rnd == 1: 
      n = random.randint(n / 2, n * 2) 
      if n <= 0: n = 1 
    dx = self.top.x - self.bottom.x 
    dy = self.top.y - self.bottom.y 
    r = 0.20 + random.random() * 0.2 
    if self.top.x == self.bottom.x: 
      # 如果是一条竖线 
      x = self.top.x 
      y = dy * r + self.bottom.y 
    elif self.top.y == self.bottom.y: 
      # 如果是一条横线 
      x = dx * r + self.bottom.x 
      y = self.top.y 
    else: 
      x = dx * r 
      y = x * dy / dx 
      x += self.bottom.x 
      y += self.bottom.y 
    oldTop = self.top 
    self.top = Point(x, y) 
    a = math.pi / (2 * n) 
    for i in range(n): 
      a2 = -a * (n - 1) / 2 + a * i - math.pi 
      a2 *= 0.9 + random.random() * 0.2 
      self.children.append(self.mkNewBranch(self.top, oldTop, a2)) 
 
  def mkNewBranch(self, bottom, top, a): 
    dx1 = top.x - bottom.x 
    dy1 = top.y - bottom.y 
    r = 0.9 + random.random() * 0.2 
    c = math.sqrt(dx1 ** 2 + dy1 ** 2) * r 
    if dx1 == 0: 
      a2 = math.pi / 2 
    else: 
      a2 = math.atan(dy1 / dx1) 
      if (a2 < 0 and bottom.y > top.y) / 
        or (a2 > 0 and bottom.y < top.y) / 
        : 
        a2 += math.pi 
    b = a2 - a 
    dx2 = c * math.cos(b) 
    dy2 = c * math.sin(b) 
    newTop = Point(dx2 + bottom.x, dy2 + bottom.y) 
    return Branch(bottom, newTop, self.branches, self.level + 1) 
 
class Tree(object): 
  def __init__(self, root, canvas, bottom, top, branches = 3, depth = 3): 
    self.root = root 
    self.canvas = canvas 
    self.bottom = bottom 
    self.top = top 
    self.branches = branches 
    self.depth = depth 
    self.new() 
 
  def gen(self, n = 1): 
    for i in range(n): 
      self.getLeaves() 
      for node in self.leaves: 
        node.nextGen() 
    self.show() 
 
  def new(self): 
    self.leavesCount = 0 
    self.branch = Branch(self.bottom, self.top, self.branches) 
    self.gen(self.depth) 
    print "leaves count: %d" % self.leavesCount 
 
  def chgDepth(self, d): 
    self.depth += d 
    if self.depth < 0: self.depth = 0 
    if self.depth > 10: self.depth = 10 
    self.new() 
 
  def chgBranch(self, d): 
    self.branches += d 
    if self.branches < 1: self.branches = 1 
    if self.branches > 10: self.branches = 10 
    self.new() 
 
  def getLeaves(self): 
    self.leaves = [] 
    self.map(self.findLeaf) 
 
  def findLeaf(self, node): 
    if len(node.children) == 0: 
      self.leaves.append(node) 
 
  def show(self): 
    for i in self.canvas.find_all(): 
      self.canvas.delete(i) 
    self.map(self.drawNode) 
    self.canvas.tag_raise("leaf") 
 
  def exit(self, evt): 
    sys.exit(0) 
 
  def map(self, func = lambda node: node): 
    # 遍历树 
    children = [self.branch] 
    while len(children) != 0: 
      newChildren = [] 
      for node in children: 
        func(node) 
        newChildren.extend(node.children) 
      children = newChildren 
 
  def drawNode(self, node): 
    self.line2( 
#    self.canvas.create_line( 
        node.bottom.x, 
        node.bottom.y, 
        node.top.x, 
        node.top.y, 
        fill = "#100", 
        width = 1.5 ** (self.depth - node.level), 
        tags = "branch level_%d" % node.level, 
      ) 
 
    if len(node.children) == 0: 
      # 画叶子 
      self.leavesCount += 1 
      self.canvas.create_oval( 
          node.top.x - 3, 
          node.top.y - 3, 
          node.top.x + 3, 
          node.top.y + 3, 
          fill = "#090", 
          tag = "leaf", 
        ) 
 
    self.canvas.update() 
 
  def line2(self, x0, y0, x1, y1, width = 1, fill = "#000", minDist = 10, tags = ""): 
    dots = midDots(x0, y0, x1, y1, minDist) 
    dots2 = [] 
    for i in range(len(dots) - 1): 
      dots2.extend([dots[i].x, 
        dots[i].y, 
        dots[i + 1].x, 
        dots[i + 1].y]) 
    self.canvas.create_line( 
        dots2, 
        fill = fill, 
        width = width, 
        smooth = True, 
        tags = tags, 
      ) 
 
def midDots(x0, y0, x1, y1, d): 
  dots = [] 
  dx, dy, r = x1 - x0, y1 - y0, 0 
  if dx != 0: 
    r = float(dy) / dx 
  c = math.sqrt(dx ** 2 + dy ** 2) 
  n = int(c / d) + 1 
  for i in range(n): 
    if dx != 0: 
      x = dx * i / n 
      y = x * r 
    else: 
      x = dx 
      y = dy * i / n 
    if i > 0: 
      x += d * (0.5 - random.random()) * 0.25 
      y += d * (0.5 - random.random()) * 0.25 
    x += x0 
    y += y0 
    dots.append(Point(x, y)) 
  dots.append(Point(x1, y1)) 
  return dots 
 
if __name__ == "__main__": 
  root = Tkinter.Tk() 
  root.title("Tree") 
  gw, gh = 800, 600 
  canvas = Tkinter.Canvas(root, 
      width = gw, 
      height = gh, 
    ) 
  canvas.pack() 
  tree = Tree(root, canvas, Point(gw / 2, gh - 20), Point(gw / 2, gh * 0.2), / 
    branches = 2, depth = 8) 
  root.bind("n", lambda evt: tree.new()) 
  root.bind("=", lambda evt: tree.chgDepth(1)) 
  root.bind("+", lambda evt: tree.chgDepth(1)) 
  root.bind("-", lambda evt: tree.chgDepth(-1)) 
  root.bind("b", lambda evt: tree.chgBranch(1)) 
  root.bind("c", lambda evt: tree.chgBranch(-1)) 
  root.bind("q", tree.exit) 
  root.mainloop()

因为每次生成的树都是随机的,所以你生成的树和上图会不太一样,可能会更为枝繁叶茂,也可能会看起来才刚刚发芽。程序中绑定了若干快捷键,比如“n”是随机产生一颗新的树,“q”是退出程序。另外还有一些不太常用的快捷键,如“+”/“-”是增加/减少树的深度,“b”/“c”分别代表更多/更少的分叉,需要注意的是,增加深度或分叉可能需要更多的计算时间。

从这次树形图案的绘制过程中,我也有一些有趣的发现,比如,树枝上某一处的横截面宽度与它与树根之间的距离似乎呈一种指数函数的关系。如用H表示树的总高度,h表示树枝上某一点的高度,w表示这一点横截面的宽度,那么w与h之间似乎存在这样一种关系:w = a * b ^ (H - h) + c,这儿a、b、c都是常数。当然,这只是一个猜测,因为绘制的过程中我发现当w与h满足这样关系时画出来的图案看起来最“自然”,这个问题或许下次可以再深入研究一下。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
python&amp;MongoDB爬取图书馆借阅记录
Feb 05 Python
python批量添加zabbix Screens的两个脚本分享
Jan 16 Python
python数据类型_字符串常用操作(详解)
May 30 Python
Django ORM框架的定时任务如何使用详解
Oct 19 Python
Python reduce()函数的用法小结
Nov 15 Python
基于python代码实现简易滤除数字的方法
Jul 17 Python
Python自定义一个类实现字典dict功能的方法
Jan 19 Python
用openCV和Python 实现图片对比,并标识出不同点的方式
Dec 19 Python
python lambda函数及三个常用的高阶函数
Feb 05 Python
快速解决Django关闭Debug模式无法加载media图片与static静态文件
Apr 07 Python
Python实现自动签到脚本的示例代码
Aug 19 Python
python之django路由和视图案例教程
Jul 26 Python
Python爬取十篇新闻统计TF-IDF
Jan 03 #Python
Python制作词云的方法
Jan 03 #Python
Python读取Json字典写入Excel表格的方法
Jan 03 #Python
python基于ID3思想的决策树
Jan 03 #Python
python遍历文件夹下所有excel文件
Jan 03 #Python
Python将多份excel表格整理成一份表格
Jan 03 #Python
Python将多个excel文件合并为一个文件
Jan 03 #Python
You might like
php+resumablejs实现的分块上传 断点续传功能示例
2017/04/18 PHP
PHP+Ajax实现的检测用户名功能简单示例
2019/02/12 PHP
javascript 设置某DIV区域内的checkbox复选框
2009/11/30 Javascript
brook javascript框架介绍
2011/10/10 Javascript
jquery果冻抖动效果实现方法
2015/01/15 Javascript
微信小程序 WXDropDownMenu组件详解及实例代码
2016/10/24 Javascript
详解vue-validator(vue验证器)
2017/01/16 Javascript
详解NODEJS的http实现
2018/01/04 NodeJs
vue将对象新增的属性添加到检测序列的方法
2018/02/24 Javascript
javascript 模块依赖管理的本质深入详解
2020/04/30 Javascript
javascript实现贪吃蛇小游戏
2020/07/28 Javascript
python模块restful使用方法实例
2013/12/10 Python
Python爬取读者并制作成PDF
2015/03/10 Python
python+matplotlib演示电偶极子实例代码
2018/01/12 Python
对python中的logger模块全面讲解
2018/04/28 Python
解决pycharm每次新建项目都要重新安装一些第三方库的问题
2019/01/17 Python
对python中矩阵相加函数sum()的使用详解
2019/01/28 Python
详解Django 时间与时区设置问题
2019/07/23 Python
python Django编写接口并用Jmeter测试的方法
2019/07/31 Python
python实现XML解析的方法解析
2019/11/16 Python
python实现将列表中各个值快速赋值给多个变量
2020/04/02 Python
浅析Python 条件控制语句
2020/07/15 Python
如何使用scrapy中的ItemLoader提取数据
2020/09/30 Python
武汉瑞得软件笔试题
2015/10/27 面试题
汽车电子与维修专业大学生求职信
2013/09/28 职场文书
国贸专业个人求职信分享
2013/12/04 职场文书
在校生自我鉴定
2014/01/23 职场文书
爱与责任演讲稿
2014/05/20 职场文书
社区创先争优承诺书
2014/08/30 职场文书
公务员学习习总书记“三严三实”思想汇报
2014/09/19 职场文书
法学专业大学生实习自我鉴定
2014/10/05 职场文书
派出所正风肃纪剖析材料
2014/10/10 职场文书
教师三严三实学习心得体会
2014/10/11 职场文书
影视后期实训报告
2014/11/05 职场文书
CSS3 制作的图片滚动效果
2021/04/14 HTML / CSS
Pytorch 统计模型参数量的操作 param.numel()
2021/05/13 Python