# Python 中的分形樹

| | | | | |

`pip install pygame `

` # 導入python庫 `

` import ` ` pygame, math `

` # 初始化 cue 所有導入的 Pygame 模塊 `
` pygame. init () `

` # 創建一個新的表面和窗口。 `

`surface_height,surface_width ` `=` `800` code> `, ` ` 600 ` ` # 表面變量 `

` main_surface ` ` = ` ` pygame.display.set_mode ((surface_height, surface_width)) `

` `
` # 窗口標題`

` pygame.display.set_caption (` ` "Fractal_Tree_pythonengineering" ` `) `

` def ` ` draw_tree (order, theta, sz, pos n, heading, color ` ` = ` ` (` ` 0 ` `, ` ` 0 ` , ` 0 ` `), depth ` ` = ` ` 0 ` `): `

`相對樹幹與整棵樹的比例 `

` ` ` trunk_ratio ` ` = ` ` 0.29 `

` #桶長`

` trunk ` ` = ` ` sz ` ` * ` ` trunk_ratio `

` delta_x ` ` = ` `trunk ` ` * ` ` math.cos（標題）`

` delta_y ` ` = ` ` trunk ` ` * ` ` math.sin (heading) `

` ` ` (u, v) ` ` = ` ` posn `

` newpos ` ` = ` ` (u + delta_x, v + delta_y) `

` pygame.draw.line (main_surface, color , posn, newpos) `

` if order > 0 : #再畫一層子樹 # 接下來六行 - 簡單的 hack # 遞歸的兩個主要部分是不同的 # 顏色。在這裡撥弄以將顏色更改為另一個 # 深度，或者當深度為偶數或奇數時等。 if depth = = 0 : `

` <代碼類="plain "> color1 ` ` = ` ` (` ` 255 ` `, ` ` 0 ` `, ` ` 0 ` `) `

` color2 ` ` = ` ` ( ` ` 0 ` `, ` ` 0 ` `, ` ` 255 ` `) `

` else ` `: `

` color1 ` ` = ` ` color `

` color2 ` ` = ` ` color `

` # 遞歸調用繪製兩個子樹 `

` newsz ` ` = ` ` sz ` ` * ` ` (` ` 1 ` ` - ` ` trunk_ratio) `

` draw_tree (order ` ` - ` ` 1 ` `, theta, newsz, newpos, heading ` ` - ` ` theta, color1, depth ` ` + ` ` 1 ` `) `

` draw_tree (order ` ` - ` ` 1 ` `, theta, newsz, newpos, heading ` ` + ` ` theta, color2, depth ` ` + ` ` 1 ` `) `

` def ` ` main(): `

` theta ` ` = ` ` 0 `

` `

` while ` ` True ` `: `

` ` ` # 更新角 `

` theta ` ` + ` ` = ` ` 0.01 `

` `

` ` ` # 這個小部分讓我們畫東西 `

` #屏幕上的所有內容 `

` main_surface.fill ((` ` 255 ` `, ` ` 255 ` `, ` ` 0 ` `)) `

` ` ` draw_tree (` ` 9 ` `, theta, surface_height ` ` * ` ` 0.9 ` `, (surface_width ` ` / ` ` / ` ` 2 ` `, surface_width ` ` - ` ` 50 ` `), ` ` - ` ` math.pi ` ` / ` ` 2 ` ` ) `

` pygame.display.flip () `

` #調用main函數`
`main()`
`pygame.quit()`

`% MINIF YHTMLc707a572199fb7b4b8a82374bf37b87213%      `

## Shop

Learn programming in R: courses

\$

Best Python online courses for 2022

\$

Best laptop for Fortnite

\$

Best laptop for Excel

\$

Best laptop for Solidworks

\$

Best laptop for Roblox

\$

Best computer for crypto mining

\$

Best laptop for Sims 4

\$

Latest questions

NUMPYNUMPY

psycopg2: insert multiple rows with one query

NUMPYNUMPY

How to convert Nonetype to int or string?

NUMPYNUMPY

How to specify multiple return types using type-hints

NUMPYNUMPY

Javascript Error: IPython is not defined in JupyterLab

## Wiki

Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | cv2.circle () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method