用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字

Python自学    -  云代码空间

—— python自学——怎么学习python,自学python要多久?python+人工智能+人工智能+人工智能+大数据分析

Python画标准国旗

2022-10-01|728阅||

摘要:预备知识 网上搜的国旗设计图: 假设国旗中心点为坐标系原点(0,0),每个格子长宽为L): o    国旗的四个顶点为(-15L,10L),(15L,10L),(15L,-10L),(-15L,


1. 预备知识

网上搜的国旗设计图:



假设国旗中心点为坐标系原点(0,0),每个格子长宽为L):

  • o    国旗的四个顶点为(-15L,10L),(15L,10L),(15L,-10L),(-15L,-10L)
  • o    大五角星中心点为(-10L,5L),半径为3L,水平放置
  • o    小五角星A中心点为(-5L,8L),半径为L,tan(A)=3/5
  • o    小五角星B中心点为(-3L,6L),半径为L,tan(B)=1/7
  • o    小五角星C中心点为(-3L,3L),半径为L,tan(C)=2/7
  • o    小五角星D中心点为(-5L,1L),半径为L,tan(D)=4/5


上面的三角函数用于计算4颗五角星的旋转角度。

turtle库:

turtle库是Python语言中一个很流行的绘制图像的函数库,想象一个小乌龟,在一个横轴为x、纵轴为y的坐标系原点,(0,0)位置开始,根据一组函数指令的控制,在平面坐标系中移动,从而在它爬行的路径上绘制了图形。

turtle库常用方法:

  • pensize() 画笔的粗细
  • speed()  画笔移动的速度  范围是1-10最快和0直接成图
  • color() 颜色
  • forward(x) 朝着画笔方向移动x像素
  • backward(x) 朝着画笔的反方向移动
  • right(degree)  顺时针转动degree度
  • left(degree)   逆时针转动degree度
  • up()  提起画笔移动
  • down() 落笔
  • goto(x,y) 将画笔移动到坐标为(x,y)的位置
  • begin_fill() 开始填充
  • end_fill() 结束填充



2. 画旗面函数封装,矩形,填充红色


# 绘制长方形
def rectangle(center, length_x, length_y, penc, fillc):
    turtle.pensize(1)
    turtle.fillcolor(fillc)
    turtle.pencolor(penc)
    turtle.up()
    pos = (center[0] - length_x / 2, center[1] + length_y / 2)
    turtle.goto(pos)
    turtle.begin_fill()
    turtle.down()
    turtle.seth(0)
    turtle.fd(length_x)
    turtle.seth(-90)
    turtle.fd(length_y)
    turtle.seth(180)
    turtle.fd(length_x)
    turtle.seth(90)
    turtle.fd(length_y)
    turtle.end_fill()


3. 画五角星函数封装,填充黄色


# 绘制星星
def star(center, angle, length, penc, fillc):
    turtle.pensize(1)
    turtle.fillcolor(fillc)
    turtle.pencolor(penc)
    L = length * math.sin(36 * math.pi / 180) / math.sin(54 * math.pi / 180)
    turtle.up()
    turtle.goto(center)
    turtle.seth(90 + angle)
    turtle.fd(length)
    turtle.seth(180 + 72 + angle)
    turtle.down()
    turtle.begin_fill()
    for _ in range(5):
        turtle.fd(L)
        turtle.right(72)
        turtle.fd(L)
        turtle.left(144)
    turtle.end_fill()


上面画五角星方法是沿着边重复画,还有另外一种方法,类似平时我们手绘的路径,最后填充
 
import turtle
import time

turtle.pensize(5)
turtle.pencolor("yellow")
turtle.fillcolor("yellow")

turtle.begin_fill()

for _ in range(5):
    turtle.forward(200)
    turtle.right(144)
turtle.end_fill()
time.sleep(2)

turtle.penup()
turtle.goto(-150, -120)
turtle.color("violet")
time.sleep(2)


4. 调用方法


W = 1200
H = 800
dW = W / 30
dH = H / 20
C0 = (-dW * 10, dH * 5)
A0 = 0 #旋转角度,国旗上的4颗小五角星各有一个尖对着大五角星
L0 = dW * 3
#画大五角星
star(C0, A0, L0, 'yellow', 'yellow')



先画大五角星,然后变更坐标点,调整A0旋转角度分别画4颗小五角星。


5. 整体代码,可直接运行


# 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("white")
turtle.speed(10)
turtle.pensize(1)


# 绘制星星
def star(center, angle, length, penc, fillc):
    turtle.pensize(1)
    turtle.fillcolor(fillc)
    turtle.pencolor(penc)
    L = length * math.sin(36 * math.pi / 180) / math.sin(54 * math.pi / 180)
    turtle.up()
    turtle.goto(center)
    turtle.seth(90 + angle)
    turtle.fd(length)
    turtle.seth(180 + 72 + angle)
    turtle.down()
    turtle.begin_fill()
    for _ in range(5):
        turtle.fd(L)
        turtle.right(72)
        turtle.fd(L)
        turtle.left(144)
    turtle.end_fill()


# 绘制长方形
def rectangle(center, length_x, length_y, penc, fillc):
    turtle.pensize(1)
    turtle.fillcolor(fillc)
    turtle.pencolor(penc)
    turtle.up()
    pos = (center[0] - length_x / 2, center[1] + length_y / 2)
    turtle.goto(pos)
    turtle.begin_fill()
    turtle.down()
    turtle.seth(0)
    turtle.fd(length_x)
    turtle.seth(-90)
    turtle.fd(length_y)
    turtle.seth(180)
    turtle.fd(length_x)
    turtle.seth(90)
    turtle.fd(length_y)
    turtle.end_fill()


W = 1200
H = 800
dW = W / 30
dH = H / 20

# time.sleep(6)

rectangle((0, 0), W, H, 'red', 'red')
C0 = (-dW * 10, dH * 5)

A0 = 0
L0 = dW * 3
star(C0, A0, L0, 'yellow', 'yellow')
C1 = (-dW * 5, dH * 8)
A1 = 90 + math.atan(3 / 5) * 180 / math.pi
L1 = dW
star(C1, A1, L1, 'yellow', 'yellow')
C2 = (-dW * 3, dH * 6)
A2 = 90 + math.atan(1 / 7) * 180 / math.pi
L2 = dW
star(C2, A2, L2, 'yellow', 'yellow')
C3 = (-dW * 3, dH * 3)
A3 = 90 - math.atan(2 / 7) * 180 / math.pi
L3 = dW
star(C3, A3, L3, 'yellow', 'yellow')
C4 = (-dW * 5, dH)
A4 = 90 - math.atan(4 / 5) * 180 / math.pi
L4 = dW
star(C4, A4, L4, 'yellow', 'yellow')

turtle.hideturtle()
turtle.done()


运行效果


顶 7踩 3收藏
文章评论
    发表评论