最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python如何生成樹形圖案

 更新時間:2018年01月03日 11:49:15   作者:oldjwu  
這篇文章主要為大家詳細(xì)介紹了Python如何生成樹形圖案,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了Python生成樹形圖案的具體代碼,供大家參考,具體內(nèi)容如下

先看一下效果,見下圖。

上面這顆大樹是使用Python + Tkinter繪制的,主要原理為使用分形畫樹干、樹枝,最終葉節(jié)點(diǎn)上畫上綠色圓圈代表樹葉。當(dāng)然,為了看起來更真實,繪制過程中也加入了一些隨機(jī)變化,比如樹枝會稍微有些扭曲而不是一條直線,分叉的角度、長短等都會隨機(jī)地作一些偏移等。

以下是完整源代碼:

# -*- 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() 

  因為每次生成的樹都是隨機(jī)的,所以你生成的樹和上圖會不太一樣,可能會更為枝繁葉茂,也可能會看起來才剛剛發(fā)芽。程序中綁定了若干快捷鍵,比如“n”是隨機(jī)產(chǎn)生一顆新的樹,“q”是退出程序。另外還有一些不太常用的快捷鍵,如“+”/“-”是增加/減少樹的深度,“b”/“c”分別代表更多/更少的分叉,需要注意的是,增加深度或分叉可能需要更多的計算時間。

  從這次樹形圖案的繪制過程中,我也有一些有趣的發(fā)現(xiàn),比如,樹枝上某一處的橫截面寬度與它與樹根之間的距離似乎呈一種指數(shù)函數(shù)的關(guān)系。如用H表示樹的總高度,h表示樹枝上某一點(diǎn)的高度,w表示這一點(diǎn)橫截面的寬度,那么w與h之間似乎存在這樣一種關(guān)系:w = a * b ^ (H - h) + c,這兒a、b、c都是常數(shù)。當(dāng)然,這只是一個猜測,因為繪制的過程中我發(fā)現(xiàn)當(dāng)w與h滿足這樣關(guān)系時畫出來的圖案看起來最“自然”,這個問題或許下次可以再深入研究一下。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Python 訪問限制 private public的詳細(xì)介紹

    Python 訪問限制 private public的詳細(xì)介紹

    在一個模塊中,我們可能會定義很多函數(shù)和變量。這篇文章主要介紹了Python 訪問限制 private public的詳細(xì)介紹,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-10-10
  • python神經(jīng)網(wǎng)絡(luò)編程實現(xiàn)手寫數(shù)字識別

    python神經(jīng)網(wǎng)絡(luò)編程實現(xiàn)手寫數(shù)字識別

    這篇文章主要為大家詳細(xì)介紹了python神經(jīng)網(wǎng)絡(luò)編程實現(xiàn)手寫數(shù)字識別,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-05-05
  • python tkinter canvas使用實例

    python tkinter canvas使用實例

    這篇文章主要介紹了python tkinter canvas使用實例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2019-11-11
  • 使用Python在Excel中設(shè)置和自定義頁眉和頁腳的操作方法

    使用Python在Excel中設(shè)置和自定義頁眉和頁腳的操作方法

    本文介紹如何使用Python在Excel中設(shè)置和自定義頁眉和頁腳,內(nèi)容包括插入基礎(chǔ)頁眉和頁腳、在頁眉和頁腳中添加圖片、為奇偶頁設(shè)置不同的頁眉和頁腳,以及為第一頁設(shè)置不同的頁眉和頁腳,感興趣的小伙伴跟著小編一起來看看吧
    2025-02-02
  • Python網(wǎng)絡(luò)爬蟲與信息提取(實例講解)

    Python網(wǎng)絡(luò)爬蟲與信息提取(實例講解)

    下面小編就為大家?guī)硪黄狿ython網(wǎng)絡(luò)爬蟲與信息提取(實例講解)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-08-08
  • python實現(xiàn)猜拳游戲項目

    python實現(xiàn)猜拳游戲項目

    這篇文章主要為大家詳細(xì)介紹了python實現(xiàn)猜拳游戲項目,以excel形式保存信息,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-11-11
  • python 通過類中一個方法獲取另一個方法變量的實例

    python 通過類中一個方法獲取另一個方法變量的實例

    今天小編就為大家分享一篇python 通過類中一個方法獲取另一個方法變量的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-01-01
  • Python?OpenCV實現(xiàn)簡單的顏色識別功能(對紅色和藍(lán)色識別并輸出)

    Python?OpenCV實現(xiàn)簡單的顏色識別功能(對紅色和藍(lán)色識別并輸出)

    Python?OpenCV可以用來進(jìn)行顏色識別,可以通過讀取圖像的像素值,來判斷像素點(diǎn)的顏色,從而實現(xiàn)顏色識別,這篇文章主要給大家介紹了關(guān)于Python?OpenCV實現(xiàn)簡單的顏色識別功能(對紅色和藍(lán)色識別并輸出)的相關(guān)資料,需要的朋友可以參考下
    2023-12-12
  • Python序列對象與String類型內(nèi)置方法詳解

    Python序列對象與String類型內(nèi)置方法詳解

    這篇文章主要介紹了Python序列對象與String類型內(nèi)置方法,結(jié)合實例形式分析了Python序列對象與String類型各種常見內(nèi)置方法相關(guān)使用技巧及操作注意事項,需要的朋友可以參考下
    2019-10-10
  • 使用python?itertools實現(xiàn)計算雙十一滿減湊單

    使用python?itertools實現(xiàn)計算雙十一滿減湊單

    一年一度的雙十一又到了,在這樣一個日子中,可能遇到一些問題,首先是“湊單”問題,本文將使用python中的itertools庫解決這一問題,感興趣的可以了解下
    2024-11-11

最新評論

宁化县| 福州市| 宁德市| 泾川县| 静乐县| 静乐县| 邵阳县| 南康市| 兰坪| 新化县| 中牟县| 隆尧县| 海口市| 云林县| 神池县| 页游| 桓仁| 句容市| 尼勒克县| 土默特右旗| 浏阳市| 永年县| 永善县| 水富县| 闽侯县| 齐河县| 福清市| 乐至县| 灯塔市| 鄂伦春自治旗| 密云县| 康马县| 鄂尔多斯市| 海城市| 古丈县| 班戈县| 广安市| 都安| 洛阳市| 正宁县| 凤城市|