python and fractal 0006 - [tutorial] rotating lines

Back to our fractal tutorial, we use python's turtle module, which comes with the installation of python.

Open the IDE that comes with python and press F1 to help search for turtle to find the help document.

Turtle graphics is a popular way for introducing programming to kids. It was part of the original Logo programming language developed by Wally Feurzeig, Seymour Papert and Cynthia Solomon in 1967. ...

This was originally a tool to help children learn programming. Later, it was transplanted to python. Its basic idea is to simulate a little turtle walking, so as to draw various graphics.

There are netizens who draw all kinds of strange graphics on the Internet. You can go and have a look.

In particular, it also contains polar coordinates, so you can make all kinds of difficult and cool actions and colorful colors.

I roughly counted the API included in the turtle module. There are almost 100 functions, and less than 20 are commonly used; As long as you can program, it takes 4.9 minutes to get started.

Today's tutorial is a rotating straight line, and its results are as follows:

Rotating line

This graphic looks simple, but like the "Yong" in Chinese characters, it contains all the elements required for turtle drawing: configuration, basic graphics, color and animation.

to configure

Configuration refers to the configuration of the turtle canvas, including the size of the canvas, the background color, the coordinate system, etc. generally, I often use the following functions.

Hide the little turtle brush. The little arrow looks annoying:

turtle.hideturtle()
turtle.ht()

Set the background color of the tablecloth. I generally prefer black (turtle.bgcolor("black"):

turtle.bgcolor(*args)

Modify the drawing speed. If you want the graph to render and display the final result immediately, I use turtle.tracer(0, 0):

turtle.tracer(n=None, delay=None)

Set the size of the tablecloth window. Use the setup function. I generally use the percentage for width and height, 1.0 for full screen, and None for startx and starty for center:

turtle.setup(width=_CFG["width"], height=_CFG["height"], startx=_CFG["leftright"], starty=_CFG["topbottom"])

turtle.setup(width=1.0, height=1.0, startx=None, starty=None)

Basic graphics and colors

What are we going to do? Draw a colored line.

When we want to draw a straight line, what do we do?

  • prepare: select a pen of appropriate thickness and color.
  • step 1: write.
  • step 2: move the pen in a certain direction.
  • step 3: stop writing when the required length is reached.
  • step 4: start writing.

What do you do in the program? Please match the above one by one.

  • prepare: turtle.pensize(3) turtle.pencolor('red')
  • step 1: turtle.pendown()
  • step 2: turtle.seth(angle)
  • step 3: turtle.forward(length)
  • step 4: turtle.penup()

Code Description:

function

explain

turtle.pensize

Brush thickness

turtle.pencolor

stroke color

turtle.pendown

Write

turtle.penup

Start writing

turtle.setheading,turtle.seth

Set brush angle

turtle.forward,turtle.fd

Move forward for example

Integrated into code, that is:

def draw_line(length, angle):
    turtle.pensize(3)
    turtle.pencolor('red')
    turtle.pendown()
    turtle.seth(angle)
    turtle.fd(length)
    turtle.penup()

animation

The basic principle of animation is to play pictures more frequently than human eyes can distinguish, right.

How can we make a straight line move.

  • step 1: clear the screen.
  • step 2: draw a straight line.
  • step 3: refresh the interface.
  • step 4: wait for x seconds (1/X is the refresh rate).
  • step 5: repeat steps 1, 2, 3 and 4.

Isn't it simple.

so easy

What does the code do?

  • step 1: turtle.clear() turtle.goto(0, 0)
  • step 2: draw_line(400, angle)
  • step 3: turtle.update()
  • step 4: time.sleep(0.05)
  • step 5: while for

Code Description:

function

explain

turtle.clear

Clear screen

turtle.goto(0, 0)

Back to the origin

turtle.update

Refresh the image when tracer is closed

time.sleep()

wait for

Is it super simple? If you integrate the above code, you can draw a rotating straight line.

# coding: utf-8

import turtle
import time

turtle.setup(width=1.0, height=1.0, startx=None, starty=None)
turtle.ht()
turtle.tracer(0, 0)
turtle.bgcolor("black")

def draw_line(length, angle):
    turtle.pensize(3)
    turtle.pencolor('red')
    turtle.pendown()
    turtle.seth(angle)
    turtle.fd(length)
    turtle.penup()
    
for angle in range(3600):
    turtle.clear()
    turtle.goto(0, 0)
    draw_line(400, angle)
    turtle.update()
    time.sleep(0.05)

Advanced

In the function of turtle, there is a timer callback function, which is described in the manual as follows:

turtle.ontimer(fun, t=0) Install a timer that calls fun after t milliseconds.

So, we can call this function in our drawing function, and we can create infinite loop by recursively drawing function.

Let's make a simple transformation of the above function:

# coding: utf-8

import turtle
import time
import math

turtle.setup(width=1.0, height=1.0, startx=None, starty=None)
turtle.ht()
turtle.tracer(0, 0)
turtle.bgcolor("black")

angle=1
length=400

def draw_line():
    global angle
    turtle.clear()
    turtle.goto(0, 0)
    turtle.pensize(3)
    turtle.pencolor('red')
    turtle.pendown()
    turtle.fd(length)
    turtle.seth(angle)
    angle += 1
    if angle > 360:
        angle = 0
    turtle.penup()
    turtle.ontimer(draw_line, 50)
    
draw_line()

At this point, a color rotatable straight line is ready. Is it super simple.

With this foundation, we can make some changes, such as rotating color disc, rotating Tai Chi, etc. we will do it later.

Posted on Fri, 12 Nov 2021 09:08:53 -0500 by kiwi_uk