python繪製天然雪花結晶

python繪製天然雪花結晶,第1张

python繪製天然雪花結晶

本篇主要是討論用 python 模擬自然界的雪花結晶形狀, (注意: 非網路上常提到的 雪花算法).

“Talk is cheap. Show me the code.”
― Linus Torvalds

老子第41章
上德若谷
大白若辱
大方無隅
大器晚成
大音希聲
大象無形
道隱無名

拳打千遍, 身法自然

“There’s no shortage of remarkable ideas, what’s missing is the will to execute them.” – Seth Godin
「很棒的點子永遠不會匱乏,然而缺少的是執行點子的意志力。」—賽斯.高汀


文章目录
  • 樹枝雪花
    • 參考 GeekGurlDiaries 的程式碼
      • 分解說明
        • branch()
        • snowflake()
        • 最後段: 畫多個隨機位置、大小、顏色之雪花
    • 修改 GeekGurlDiaries 的程式碼
      • 雪花的瓣數確定
      • 雪花的瓣數也隨機決定
  • 蕨葉雪花
  • Referencs


本篇主要是討論用 python 模擬自然界的雪花結晶形狀, (注意: 非網路上常提到的 雪花算法).

樹枝雪花 參考 GeekGurlDiaries 的程式

此處我們先參考以下 GeekGurlDiaries YouTube 上影片的程式碼:
Ref: How to make Snowflakes with Code (Xmas Hour of Code Special) link

# Geek Gurl Diaries Episode 33: Xmas Special Make Snowflakes with Turtle
# By Carrie Anne Philbin
# https://www.youtube.com/watch?v=DHmeX7YTHBY

import turtle
import random

# setup the window with a background colour
wn = turtle.Screen()
wn.bgcolor("cyan")

# assign a name to your turtle
elsa = turtle.Turtle()
elsa.speed(15)

# create a list of colours
sfcolor = ["white", "blue", "purple", "grey", "magenta"]

# create a function to create different size snowflakes
def snowflake(size):
# move the pen into starting position
    elsa.penup()
    elsa.forward(10*size)
    elsa.left(45)
    elsa.pendown()
    elsa.color(random.choice(sfcolor))
    # draw branch 8 times to make a snowflake
    for i in range(8):
        branch(size)   
        elsa.left(45)
    

# create one branch of the snowflake
def branch(size):
    for i in range(3):
        for i in range(3):
            elsa.forward(10.0*size/3)
            elsa.backward(10.0*size/3)
            elsa.right(45)
        elsa.left(90)
        elsa.backward(10.0*size/3)
        elsa.left(45)
    elsa.right(90) 
    elsa.forward(10.0*size)

# loop to create 20 different sized snowflakes with different starting co-ordinates
for i in range(20):
    x = random.randint(-200, 200)
    y = random.randint(-200, 200)
    sf_size = random.randint(1, 4)
    elsa.penup()
    elsa.goto(x, y)
    elsa.pendown()
    snowflake(sf_size)

# leave the window open until you click to close  
wn.exitonclick()  

效果如下:

分解說明 branch()

我們看到, 主要是透過 branch() 繪製每個枝葉, 繪出8個主要枝葉, 間格 45度, 就完成雪花,

我們將程式碼簡化到只剩 branch(), 觀察他的動作

# Geek Gurl Diaries Episode 33: Xmas Special Make Snowflakes with Turtle
# By Carrie Anne Philbin
# https://www.youtube.com/watch?v=DHmeX7YTHBY

import turtle
wn = turtle.Screen()
# assign a name to your turtle
elsa = turtle.Turtle()
elsa.speed(1)
size = 20
# move the pen into starting position
elsa.penup()
elsa.forward(10*size)
elsa.left(45)
elsa.pendown()
    
# create one branch of the snowflake
def branch(size):
    for i in range(3):
        for i in range(3):
            elsa.forward(10.0*size/3)
            elsa.backward(10.0*size/3)
            elsa.right(45)
        elsa.left(90)
        elsa.backward(10.0*size/3)
        elsa.left(45)
    elsa.right(90) 
    elsa.forward(10.0*size)

branch(size)

branch(20) 繪製的效果如下:


小迴圈主要是做畫出三個小分支葉的動作
透過前進再後退, 然後右轉45度, 完成畫出一個小枝,
重複三次畫一個小枝, 組成三個小分支葉,
以下是小迴圈 codes:

for i in range(3):
    elsa.forward(10.0*size/3) 
    elsa.backward(10.0*size/3)
    elsa.right(45)

每個小迴圈畫出3個小分支,
小迴圈執行3次, 3組3個小分支葉再組成一個大分枝,
就完成整個大分枝的繪製,

他的程式碼並不好讀, 需要經常調整海龜轉向, 造成程式碼不好閱讀, 難以立即從閱讀程式碼, 就能推測出畫出的形狀.

snowflake()

snowflake() 則是呼叫8次 branch(), 完成8個大分枝組成之雪花

最後段: 畫多個隨機位置、大小、顏色之雪花

最後段則是執行20 次呼叫 snowflake() 畫雪花,
每次都重設海龜之 x, y 座標, 隨機選擇, 還有大小也是隨機選擇,
顏色之輪換則是嵌在snowflake()程式碼內,
可以看得出, 她的程式碼有多處是耦合在一起, 不好閱讀, 也不好維護修改, (許多中小學老師 Python程式能力屬於業餘的水準, 能用 python 做教案, 已經難能可貴了).

# loop to create 20 different sized snowflakes with different starting co-ordinates
for i in range(20):
    x = random.randint(-200, 200)
    y = random.randint(-200, 200)
    sf_size = random.randint(1, 4)
    elsa.penup()
    elsa.goto(x, y)
    elsa.pendown()
    snowflake(sf_size)
修改 GeekGurlDiaries 的程式碼 雪花的瓣數確定

我們將程式碼改成較專業, 海龜旋轉角度也用較直觀的方式去設計
如果我們選擇只畫出 6瓣的雪花, 類似大自然的對稱,
只畫一個,
跑出的程式碼如下圖

如果是八瓣, 20個雪花, 則圖如下

# Geek Gurl Diaries Episode 33: Xmas Special Make Snowflakes with Turtle
# By Carrie Anne Philbin
# https://www.youtube.com/watch?v=DHmeX7YTHBY
# Revised by Prof. P-J Lai MATH NKNU 20220102

# snowflake_GeekGurlDiaries_Lai.py

import turtle
import random

# setup the window with a background colour
screen = turtle.Screen()
screen.bgcolor("cyan")

# assign a name to your turtle
T = turtle.Turtle()
T.speed(0)

# create a list of colours
colorList = ["white", "blue", "purple", "grey", "magenta", "green", "yellow"]


# create one branch of the snowflake
def branch(size):
    
    for i in range(3):
        T.left(45)
        for i in range(3):
            T.forward(size/3)
            T.backward(size/3)
            T.right(45)
        T.left(90)
        T.backward(size/3)
   

# create a function to create different size snowflakes
def snowflake(size, x, y, color, petalNumber):
# move the pen into starting position
    T.penup()
    T.goto(x,y)
    T.color(color)
    T.pendown()
    # draw branch petalNumber times to make a snowflake
    for i in range(petalNumber):
        T.fd(size)
        branch(size)   
        T.left(360/petalNumber)

if __name__ == "__main__":
    petalNumber = 8
    # loop to create 20 different sized snowflakes with different starting co-ordinates
    for i in range(20):
        x = random.randint(-200, 200)
        y = random.randint(-200, 200)
        size = random.randint(10, 50)
        snowflake(size, x, y, random.choice(colorList), petalNumber)
        #snowflake(100, 0, 0, "purple", petalNumber)

雪花的瓣數也隨機決定

我們修改成, 雪花的瓣數也隨機決定,
從 6, 8, 10, 12 瓣, 隨機決定:
petalNumber = random.choice([6, 8, 10, 12])

# Geek Gurl Diaries Episode 33: Xmas Special Make Snowflakes with Turtle
# By Carrie Anne Philbin
# https://www.youtube.com/watch?v=DHmeX7YTHBY
# Revised by Prof. P-J Lai MATH NKNU 20220102

# snowflake_GeekGurlDiaries_Lai.py

import turtle
import random

# setup the window with a background colour
screen = turtle.Screen()
screen.bgcolor("cyan")

# assign a name to your turtle
T = turtle.Turtle()
T.speed(0)

# create a list of colours
colorList = ["white", "blue", "purple", "grey", "magenta", "green", "yellow"]


# create one branch of the snowflake
def branch(size):
    
    for i in range(3):
        T.left(45)
        for i in range(3):
            T.forward(size/3)
            T.backward(size/3)
            T.right(45)
        T.left(90)
        T.backward(size/3)
   

# create a function to create different size snowflakes
def snowflake(size, x, y, color, petalNumber):
# move the pen into starting position
    T.penup()
    T.goto(x,y)
    T.color(color)
    T.pendown()
    # draw branch petalNumber times to make a snowflake
    for i in range(petalNumber):
        T.fd(size)
        branch(size)   
        T.left(360/petalNumber)

if __name__ == "__main__":
    # petalNumber = 8
    # loop to create 20 different sized snowflakes with different starting co-ordinates
    for i in range(20):
        x = random.randint(-250, 250)
        y = random.randint(-250, 250)
        size = random.randint(10, 50)
        petalNumber = random.choice([6, 8, 10, 12])
        snowflake(size, x, y, random.choice(colorList), petalNumber)
        #snowflake(100, 0, 0, "purple", petalNumber)

蕨葉雪花 Referencs
  • 王燕平, 張超, 尊貴的雪花, 重慶大學, 2017.

  • 雪花算法原理_孙略 | 雪花工场 link

  • How to make Snowflakes with Code (Xmas Hour of Code Special), https://youtu.be/DHmeX7YTHBY link.
    For full code see: https://github.com/MissPhilbin/GeekGurlDiaries link

  • The snowflake man, https://youtu.be/ptLmA263hlk link

  • Snowflake Bentley, https://youtu.be/x8HYqUjbKeM link

  • Identical Snowflakes? Scientist Ruins Winter For Everyone. | Deep Look, https://youtu.be/Gojddrb70N8 link

  • The Snowflake Mystery, https://youtu.be/ao2Jfm35XeE link

  • https://blog.xuite.net/mandyl333/myself/17735392/track

欢迎分享,转载请注明来源:内存溢出

原文地址: https://www.outofmemory.cn/zaji/5689232.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存