Paint python - Snow (Koch snowflake)

Koch fractal curves which form much like snow, it is also called Koch snowflake, snowflake curve.

Here is a turtle with a python package allows us to draw a real-time

import turtle
def koch(t,n):
#定义一个函数 科赫曲线,完成绘画功能
if n < 5 :
t.fd(n)
return
m = n/3
koch(t,m)
t.lt(60)
koch(t,m)
t.rt(120)
koch(t,m)
t.lt(60)
koch(t,m)

def snowflake(t, n):
# 画一朵雪花,每一边都是一个科赫曲线
for i in range(3):
koch(t,n)
t.rt(120)

bob = turtle.Turtle()
bob.color('black')
bob.penup() # 画笔提起(不能画)
bob.goto(-150,90) #去到这个点
bob.pendown() # 画笔落下(开始画)
snowflake(bob,300) # 调用函数开始画雪花
turtle.mainloop()

Renderings:

Paint python - Snow (Koch snowflake)

 

Do not look so short code, in fact, it contains knowledge of mathematics was not easy to generate a Koch snowflake is actually a recursive process, by constantly calling koch recursion, we can form a constantly by the equilateral triangles of snow. As shown in the text of the first. More professional Wikipedia is this:

Given segment AB, Koch curve may be generated by the following steps:
1. The line segment is divided into three equal parts (the AC, CD, DB)
2. CD to a bottom, outwardly (inner and outer free) draw an equilateral triangle the DMC
. 3. the segment CD removed
4 respectively AC, CM, MD, DB repeated 1-3.
Koch snowflakes are equilateral sides of a triangle generated Koch curves. Koch length of each curve is infinite, it is nowhere to be continuous and differentiable curve.

You can also improve it, add a random function, change it pen color, form sky snow effect oh.

python technological learning exchange group: 695 185 429

Guess you like

Origin www.cnblogs.com/python0921/p/12567150.html