程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Plotting koch curve with Pythons turtle

編輯:Python

Koch curve is a kind of geometric curve like snowflake


Koch curve is a kind of fractal . Its shape is like snow , Also known as Koch snow 、 Snow curve .

  1. Given line segments AB, Koch curve can be generated by the following steps :
  2. Divide the line segment into three equal parts (AC,CD,DB)
  3. With CD Bottom , outward ( Casual inside and outside ) Draw an equilateral triangle DMC
  4. Put the line segment CD Remove ,    Respectively for AC,CM,MD,DB repeat 1~3.

import turtle
# Koch curve (size The length of each straight line of koch curve ,n Degree of drawing )
def koch(size,n):
if n==0:# The recursive exit draws a first-order line
turtle.fd(size)
else:
# Each layer recursively traverses these four angles
for angle in [0,60,-120,60]:
# Turn the angle counter clockwise
turtle.left(angle)
# Recursively call itself
koch(size/3,n-1)
def snow(a):
#turtle.color("green","yellow")
turtle.begin_fill()
# When width and height are integers , Represents pixels
turtle.screensize(600,600,"green")
# Fill the border and the middle color
turtle.color("gold","white")
# Brush up
turtle.penup()
# The width of the brush
turtle.pensize(2)
# Reach the specified location
turtle.goto(-200,100)
# Brush down , Start writing
turtle.pendown()
#a=1 #1 rank koch curve
# First jumper 0 It's time to start , Clockwise 120 Degree end draw
koch(400,a)
# Second jumper 120 It's time to start , Clockwise 240 Degree end
turtle.right(120)
# Second jumper draw
koch(400,a)
# The third jumper 240 It's time to start , Clockwise 360 Degree end
turtle.right(120)
# The third jumper draw
koch(400,a)
# Filling complete
turtle.end_fill()
# Hide the brush
turtle.hideturtle()
# Enter the number of layers of koch curve
n=input(' Please enter koch The number of layers of the curve (1):\n') #n rank koch curve
if n=='': # Without input ,1 rank koch curve
n=1
# Main function execution
snow(int(n))

Running results

Please enter koch The number of layers of the curve (1):1

Please enter koch The number of layers of the curve (1):2 

python Of turtle Supported colors

 

 

 

 


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved