Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to use python to draw pictures

Shulou Source: shulou.com Published: 2022-06-01 18:36:59 10月03日 Update

This article introduces you how to use python to draw pictures, the content is very detailed, interested friends can refer to, hope to be helpful to you.

Yesterday we wrote how to use python to draw arcs or circles. Today we draw all kinds of flowers on the basis of yesterday.

First of all, we try to add a recursive rotation angle on the basis of the arc:

Import turtle

Import math

Bob = turtle.Turtle ()

# create a turtle object named bob

Print (bob)

# draw this object

Def arc (trecom r.angle):

Arc_length = 2 * math.pi * r * angle / 360

# find the arc length

N = 50

# the number of times to draw a straight line

The higher the number of # n, the better, but the running time will increase (you can also use arc length directly)

Step_length = arc_length/n

# the length of each line drawn

Step_angle = angle/n

# the angle changed each time the line is drawn

For i in range (n):

T.fd (step_length)

# draw a straight line forward

T.lt (step_angle)

# turn the angle

T.lt (120)

# turn 120 degrees after drawing an arc

Arc (bob,200120)

# Recursive statement

Turtle.mainloop ()

# preserve graphics

Arc (bob,200,60)

# call function to realize real-time rendering

This is the clover realized by recursion.

In fact, as long as we change a small parameter, we can become such a beautiful flower. Have you guessed which step it is?

In fact, t.lt (120) becomes t.lt (90), and it automatically goes on as soon as the angle changes.

Change two parameters a little more, and it will take a handle. Hey.

If you pay attention, you will find that the flowers are all round in the middle. Which code caused this?

Python's turtle library is not so attractive, you can also use it to draw trademarks:

And beautiful snowflakes! (Koch curve, let's realize this tomorrow.)

On how to use python drawing to share here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Tags: Angle recursion one content parameter basis that is arc more straight line flower help good good-looking beautiful but not clover two code interest Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info vpn Microsoft Huawei macOS