利用turtle库绘制五星红旗——国旗

本文介绍了如何利用Python的turtle库按照国家标准尺寸绘制五星红旗,包括一个大五角星和四个小五角星的精确位置和角度计算。
摘要由CSDN通过智能技术生成

      利用turtle库等函数绘制五星红旗,显示五星红旗尺寸参考国家标准尺寸进行等比例缩小,一颗大五角星和四颗小五角星的位置和角度也需符合国家标准。

实践代码:

import turtle as t
import math
t.setup(800,500,)#显示窗口大小

def juxing(m,n):#矩形
    t.penup()
    t.goto(-m/2,-n/2)
    t.pendown()
    t.pencolor("red")
    t.begin_fill()
    t.fillcolor("red")
    for i in range (1,5):
        if i%2==1:
            a=m
        elif i%2==0:
            a=n
        t.forward(a)
        t.left(90)
    t.end_fill()

def dawujiaoxing(b):#大五角星
    t.penup()
    t.goto(-b*10,b*8)#确定位置
    t.right(72)
    t.pendown()
    d=6*b#圆的直径
    a= d * math.cos(2 * math.pi * 18 / 360)#绘制尺寸
    t.pencolor("yellow")
    t.fillcolor("yellow")
    t.begin_fill()
    for i in range(5):
         t.fd(a)
         t.right(144)
    t.end_fill()
    t.seth(0)#回到原位角度

def xiaowujiaoxing1(b):#小五角星1
    t.penup()
    x= -b * 5 - 5 * b / math.sqrt(34)
    y= b * 8 - 3 * b / math.sqrt(34)
    t.goto(x,y)#利用数学库函数确定XY的位置
    jiao= math.atan(3 / 5) / 2 / math.pi * 360 + 18
    t.left(jiao)#确定角度位置
    t.pendown()
    d=2*b
    a= d * math.cos(2 * math.pi * 18 / 360)
    t.pencolor("yellow")
    t.fillcolor("yellow")
    t.begin_fill()
    for i in range(5):#循环五次
        t.fd(a)#长度
        t.right(144)#角度
    t.end_fill()
    t.seth(0)

def xiaowujiaoxing2(b):#小五角星2
    t.penup()
    x= -b * 3 - 7 * b / math.sqrt(50)
    y= b * 6 - b / math.sqrt(50)
    t.goto(x,y)
    jiao= math.atan(1 / 7) / 2 / math.pi * 360 + 18
    t.left(jiao)
    t.pendown()
    d=2*b
    a= d * math.cos(2 * math.pi * 18 / 360)
    t.fillcolor("yellow")
    t.begin_fill()
    for i in range(5):
        t.fd(a)
        t.right(144)
    t.end_fill()
    t.seth(0)

def xiaowujiaoxing3(b):#小五角星3
    t.penup()
    x= -b * 3 - 7 * b / math.sqrt(53)
    y= b * 3 - 2 * b / math.sqrt(53)
    t.goto(x,y)
    jiao= -math.atan(2/7) / 2 / math.pi * 360 + 18
    t.left(jiao)
    t.pendown()
    d=2*b
    a= d * math.cos(2 * math.pi * 18 / 360)
    t.fillcolor("yellow")
    t.begin_fill()
    for i in range(5):
        t.fd(a)
        t.right(144)
    t.end_fill()
    t.seth(0)

def xiaowujiaoxing4(b):#小五角星4
    t.penup()
    x= -b * 5 - 5 * b / math.sqrt(41)
    y= b - 4 * b / math.sqrt(41)
    t.goto(x,y)
    jiao= -math.atan(4/5) / 2 / math.pi * 360 + 18
    t.left(jiao)
    t.pendown()
    d=2*b
    a= d * math.cos(2 * math.pi * 18 / 360)
    t.fillcolor("yellow")
    t.begin_fill()
    for i in range(5):
        t.fd(a)
        t.right(144)
    t.end_fill()
    t.seth(0)

def main():
    m=660#长
    n=m*2/3#宽
    b=n/20#小格子尺寸
    juxing(m,n)#调用前面写六个的自定义函数
    dawujiaoxing(b)
    xiaowujiaoxing1(b)
    xiaowujiaoxing2(b)
    xiaowujiaoxing3(b)
    xiaowujiaoxing4(b)
    t.hideturtle()
    t.done()
main()

运行结果:

  • 6
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Tomorrow930

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值