python drawing polygon (turtle)

Table of contents

foreword

equilateral triangle

Regular quadrilateral

regular polygon

Summarize:


foreword

The reason for this is that today I wanted OpenAI to generate an image of Luffy on a whim to see how it works, and he replied to me like this.

 I thought, is this unstable? It's so easy. result……

import turtle

# 定义画笔颜色和大小
turtle.pensize(3)
turtle.pencolor('#000')

# 设置起始点位置
turtle.penup() # 抬起画笔
turtle.goto(0, 0) # 移动到坐标原点
turtle.pendown() # 落下画笔

# 绘制头部
turtle.circle(50)

# 绘制眼睛(左眼)
turtle.up()
turtle.goto(-20, 70)
turtle.down()
turtle.pencolor('#fff')
turtle.fillcolor('#fff')
turtle.begin_fill()
turtle.circle(15)
turtle.end_fill()

# 绘制眼珠(左)
turtle.up()
turtle.goto(-20, 80)
turtle.down()
turtle.pencolor('#000')
turtle.fillcolor('#000')
turtle.begin_fill()
turtle.circle(5)
turtle.end_fill()

# 绘制眼睛(右眼)
turtle.up()
turtle.goto(20, 70)
turtle.down()
turtle.pencolor('#fff')
turtle.fillcolor('#fff')
turtle.begin_fill()
turtle.circle(15)
turtle.end_fill()

# 绘制眼珠(右)
turtle.up()
turtle.goto(20, 80)
turtle.down()
turtle.pencolor('#000')
turtle.fillcolor('#000')
turtle.begin_fill()
turtle.circle(5)
turtle.end_fill()

# 绘制鼻子
turtle.up()
turtle.goto(0, 40)
turtle.down()
turtle.setheading(-90)
turtle.pencolor('#f00')
turtle.fillcolor('#f00')
turtle.begin_fill()
turtle.forward(20)
turtle.left(90)
turtle.forward(25)
turtle.left(90)
turtle.forward(40)
turtle.left(90)
turtle.forward(25)
turtle.left(90)
turtle.forward(20)
turtle.end_fill()

# 绘制嘴巴
turtle.pencolor('#000')
turtle.up()
turtle.goto(-30, 20)
turtle.down()
turtle.setheading(-60)
turtle.circle(35, 120)

# 绘制帽子
turtle.pencolor('#f00')
turtle.fillcolor('#f00')
turtle.up()
turtle.goto(-55, 100)
turtle.down()
turtle.begin_fill()
turtle.setheading(0)
turtle.forward(110)
turtle.setheading(90)
turtle.forward(25)
turtle.setheading(180)
turtle.forward(60)
turtle.setheading(-90)
turtle.forward(50)
turtle.setheading(180)
turtle.forward(80)
turtle.end_fill()

# 隐藏海龟
turtle.hideturtle()

# 显示画面
turtle.done()

 My childhood was incomplete in an instant. Well, I definitely disagree. As a result, I asked again and finally got another answer from him.

 Wow~, I want to see how detailed it is.

import turtle

# 设置画布和画笔属性
canvas = turtle.Screen()
canvas.bgcolor("#fff")
pen = turtle.Turtle()
pen.pensize(3)
pen.speed(10)
pen.ht()

# 开始绘制路飞
pen.penup()
pen.goto(0, -150)
pen.pendown()
pen.pencolor("#000")
pen.fillcolor("#f00")
pen.begin_fill()
pen.circle(150)
pen.end_fill()

# 绘制眼睛(左眼)
pen.penup()
pen.goto(-60, 40)
pen.pendown()
pen.pencolor("#fff")
pen.fillcolor("#fff")
pen.begin_fill()
pen.circle(30)
pen.end_fill()

# 绘制眼珠(左)
pen.penup()
pen.goto(-60, 60)
pen.pendown()
pen.pencolor("#000")
pen.fillcolor("#000")
pen.begin_fill()
pen.circle(10)
pen.end_fill()

# 绘制眼睛(右眼)
pen.penup()
pen.goto(60, 40)
pen.pendown()
pen.pencolor("#fff")
pen.fillcolor("#fff")
pen.begin_fill()
pen.circle(30)
pen.end_fill()

# 绘制眼珠(右)
pen.penup()
pen.goto(60, 60)
pen.pendown()
pen.pencolor("#000")
pen.fillcolor("#000")
pen.begin_fill()
pen.circle(10)
pen.end_fill()

# 绘制嘴巴
pen.penup()
pen.goto(0, -20)
pen.pendown()
pen.pencolor("#000")
pen.right(45)
pen.circle(100, 90)

# 绘制帽子
pen.penup()
pen.goto(0, 60)
pen.pendown()
pen.pencolor("#000")
pen.fillcolor("#f00")
pen.begin_fill()
pen.right(135)
pen.forward(120)
pen.right(90)
pen.forward(60)
pen.right(90)
pen.forward(80)
pen.right(90)
pen.forward(50)
pen.right(90)
pen.forward(80)
pen.right(90)
pen.forward(60)
pen.end_fill()

# 绘制帽边
pen.penup()
pen.goto(-40, 85)
pen.pendown()
pen.pencolor("#000")
pen.right(60)
pen.fillcolor("#fff")
pen.begin_fill()
pen.forward(80)
pen.right(120)
pen.forward(80)
pen.end_fill()

# 完成绘制
canvas.exitonclick()

 I don't seem to be clean -_-, it's just that.

So I wanted to try to draw by myself. First, I had two ideas. The first was to find a good drawing figure, and the second was to learn the knowledge of turtle.

After searching for a long time, I couldn't find a simple picture. It seems that drawing a curve needs to be drawn through a circle, so I want to start by drawing a regular polygon.

equilateral triangle

Python practice combat (five) - the use of the turtle library

Several functions used here have explained their functions. This kind of straight and straight graphics is more drawing, that is, first walk a certain distance, then rotate the angle, and continue walking.

The first thing I came into contact with is the turtle graphic, and its definition is as follows.

Turtle Graphics is a vector drawing system based on simple commands such as moving forward, backward, rotating and drawing. It was originally an important part of the language logo developed by Wally Feurzeig, Seymour Papert and Cynthia Solomon in the early 1960s. In the Logo language, users can control a graphic object called a "turtle" by writing simple commands, and create graphic images by drawing straight lines, curves, etc. on the screen.

Turtle Graphics is widely used in computer graphics and computer science education. It is a simple and easy-to-understand drawing system suitable for beginners and children's education. Turtle Graphics can not only be used to draw two-dimensional graphics, but also can be extended to the drawing of three-dimensional graphics.

In modern programming languages, there are also many implementations of Turtle Graphics, such as the turtle library in the Python language. Through these libraries, users can use various commands and methods to control and draw turtle graphics, and obtain drawing effects similar to Logo language.


#画三角形
import turtle

#设置画笔粗细
turtle.pensize(3)
#设置画笔颜色
turtle.pencolor('black')

# 绘制三角形
#将海龟图形移动到画布上指定的位置(算是为了居中绘画吧)
turtle.goto(-50,0)
#在当前位置方向移动一定的距离
turtle.forward(100)
#turtle向左旋转120度
turtle.left(120)
#在当前位置方向移动一定的距离
turtle.forward(100)
#turtle向左旋转120度
turtle.left(120)
#在当前位置方向移动一定的距离
turtle.forward(100)

# 隐藏turtle图形(箭头)
turtle.hideturtle()

# 暂停程序,停止画笔绘制,但绘图窗体不关闭,直到用户关闭pythonTurtle图形化窗口为止
turtle.done()

In fact, this can also be done with a loop. The principle is the same.

# 绘制三角形
#将海龟图形移动到画布上指定的位置(算是为了居中绘画吧)
turtle.goto(-50,0)
for i in range(3):
    turtle.forward(100)
    #左转120度
    turtle.left(120)

Regular quadrilateral

For moving, some people use it turtle.fd(100) , and some people use it.  turtle.forward(100) They all mean to move the turtle forward by 100 pixels. This depends on personal habits ( personal understanding, please point out any mistakes )

#画四边形
import turtle
#设置画笔粗细
turtle.pensize(3)
#设置画笔颜色
turtle.pencolor('black')

# 绘制四边形
#将海龟图形移动到画布上指定的位置(算是为了居中绘画吧)
turtle.goto(-50,0)
for i in range(4):
    turtle.forward(100)
    # 左转90度
    turtle.left(90)

# 隐藏turtle图形(箭头)
turtle.hideturtle()

# 暂停程序,停止画笔绘制,但绘图窗体不关闭,直到用户关闭pythonTurtle图形化窗口为止
turtle.done()

regular polygon

After writing these two, I believe everyone has discovered the rules. First of all, there are many places that are the same, and then there are some differences in the loops. The number of loops is the number of sides, and the deflection angle is their outer corners.

You can first get (n-2)*180 from the internal angle sum formula of a regular polygon, and then get each internal angle ((n-2)*180)/n;

The outer corner is 180-((n-2)*180)/n=360/n;

Then just enter the number of sides n.


Encountered several problems:

Question 1: When running with PyCharm, the interface of the brush is not displayed on the top of the ground, which is very uncomfortable, so I want him to be output on the top.

How to keep PyCharm's turtle window always at the top | Qingshanlushui (huhexian.com) https://www.huhexian.com/35025.html

#设置窗口显示置顶
turtle.Screen().getcanvas().winfo_toplevel().attributes("-topmost", True)

 Question 2: I only consider the output, but not the position of the output. When the value of n is too large, the entire drawn pattern will be attached to the edge. So let the drawing position also have a relationship with n, and move down a little overall.

#将海龟图形移动到画布上指定的位置(算是为了居中绘画吧)
turtle.goto(-50,-10*n)

Question 3: Turtle.goto() will draw extra lines together. This can first lift the brush, and then put it down.

#将海龟笔尖提起
turtle.penup()
#将海龟图形移动到画布上指定的位置(算是为了居中绘画吧)
turtle.goto(-50,-10*n)
#将海龟笔尖落下
turtle.pendown()

 Full code:

#绘制正多边形
import turtle

#用户输入正多边形的边长n(我们需要的是整型的数据,所以这里需要强制转换一下)
n=int(input("请输入将绘制的多边形边长:"))
#计算外角
exteriorAngle=360/n

#设置画笔粗细
turtle.pensize(3)
#设置画笔颜色
turtle.pencolor('black')
#设置窗口显示置顶
#turtle.Screen().getcanvas()返回画布对象
#winfo_toplevel()返回turtle的主窗口
#attributes("-topmost", True)置顶显示
turtle.Screen().getcanvas().winfo_toplevel().attributes("-topmost", True)

#将海龟笔尖提起
turtle.penup()
#将海龟图形移动到画布上指定的位置(算是为了居中绘画吧)
turtle.goto(-50,-15*n)
#将海龟笔尖落下
turtle.pendown()
#循环绘制
for i in range(n):
    #移动100像素?
    turtle.forward(100)
    #左转外角的角度
    turtle.left(exteriorAngle)

# 隐藏turtle图形(箭头)
turtle.hideturtle()

# 暂停程序,停止画笔绘制,但绘图窗体不关闭,直到用户关闭pythonTurtle图形化窗口为止
turtle.done()

 When n=15, in fact, it can be designed to reduce its side length, so that a larger figure can be drawn, and the shape is closer to a circle.

 

Summarize:

This is still relatively basic, it’s all straight lines, I still want to draw it when I have time, but it seems to take time to find something, I don’t know if you guys have a better way, I still want to draw anime characters, What I see so far seems to have code, but no comments. I don’t know if it’s because the comments are useless, but the whole code is still not very friendly. The reason is that it is more interesting. Although I can't write it yet, I will still work hard towards it.

I hope it will get better and better, just like the personalized signature modified today

If you are in full bloom, butterflies will come

Guess you like

Origin blog.csdn.net/weixin_64066303/article/details/131285268