CodePudding user response:
import turtle # introduction should solution problem need tools - turtle drawing package
The parameters of the # define turtle brushes and canvas
Def huabi () :
Turtle. Speed (0) # speed () parameters have [0, 10] : "0" special, on behalf of the fastest speed, the remaining [1, 10] value, the greater the faster
Turtle. Pensize (2) # set brush width is 2
Turtle. Setup (800800, 100, 10) # setup () there are four parameters: the first two representatives of the canvas "wide" and "high", when numerical as an integer, said absolute pixel size, when the decimal, said the proportion of the screen, the latter two can be omitted, a default as the center of the screen, on behalf of the distance "left upper corner of the screen", the unit is the pixel density,
Turtle. Penup () # took up the brush, brush after action, won't produce line
Turtle. Goto (300, # 100) to center of the canvas for the origin (0, 0), move the brush to the coordinate system (300100)
Turtle. Pendown # () sets the brush to draw
# define "tip"
Def Koch (size, n) :
If n==0: # 0 order Koch snowflake curve is a line, as the input to the size of the "size"
Turtle. Fd (size)
Else: # higher-order Koch snowflake curve
For I in [0, 60, 120, 60] : # turtle in changing the Angle of the "tip" of the four line, respectively 0 °, 60 °, 129 °, 60 °
Turtle. Left (I) # corresponding to the above four Angle, total need four curve, draw the order of the four lines,
Koch (size/3, n - 1) # every Angle of a side, corresponding to the lower first-order customers snowflakes curve "tip"; Complete circulation and reuse of the function itself, automatically draw a complete order n "tip"
# define "edge"
Def sdkoch (size, n) : # complete Koch snowflake curve is composed of three "edge", we more complete is a "edge" of "tip",
Koch (size, n)
Turtle. Right (120), a group # 2 lines of code,
Koch (size, n)
Turtle. Right (120)
Koch (size, n) # at this point, to get the complete order n Koch snowflake curve
Turtle. Hideturtle () # hid turtle cursor
# end turtle turtle. Done ()
# define the main () function
Def main (size, n) : # interface to set parameters
Huabi ()
Sdkoch (size, n)
# 500, the main (2) to set the parameters, call the main function is order number 2
Looking for online