用python的turtle库画一个国旗

今天突发奇想让Chat GPT帮我画一个国旗,可是GPT画出来的星星摆的很乱,于是我就从百度上搜索标准国旗的画法:

根据标准画法再利用math库提供的三角函数计算每个星星的位置和角度,最后得出的效果如下图所示:

上代码:

import turtle
from math import radians
from math import tan
from math import atan
from math import degrees

def draw_test_line(turtle_obj,x,y,size):
    """绘制测试线"""
    turtle_obj.penup()
    turtle_obj.goto(x, y)
    turtle_obj.pendown()
    turtle_obj.forward(size)   

def draw_star(turtle_obj, x, y, size, color):
    """绘制一个星星"""
    turtle_obj.penup()
    turtle_obj.goto(x, y)
    turtle_obj.pendown()
    turtle_obj.color(color)
    turtle_obj.begin_fill()
    for _ in range(5):
        turtle_obj.forward(size)
        turtle_obj.right(144)
    turtle_obj.end_fill()

def draw_circle(turtle_obj,x,y,size):
    #绘制测试圆
    turtle_obj.penup()
    turtle_obj.goto(x,y)
    turtle_obj.pendown()
    turtle_obj.color('black')
    turtle_obj.circle(size)
    turtle_obj.color('yellow')

def draw_line(turtle_obj,x,y,length,degree):
    #绘制指向大星的圆
    turtle_obj.setheading(degree)
    turtle_obj.penup()
    turtle_obj.goto(x,y)
    turtle_obj.pendown()
    turtle_obj.color('black')
    turtle_obj.forward(length)
    turtle_obj.color('yellow')

def draw_china_flag(Auxi = True):
    width = 1020
    height = width*2/3
    screen = turtle.Screen()
    screen.setup(width=width, height=height)  # 设置长方形画布,比例为2:3
    screen.bgcolor("red")
    screen.title("China Flag")

    little_block = width/2/15

    angle = radians(18)
    angle_two = radians(36)
    angle_four = radians(72)

    """计算起始画点距离星星中心点的垂直偏移量和水平偏移量以及初始长度"""
    y = tan(angle_two)*3*little_block/(tan(angle)+tan(angle_two))
    x = 3*little_block - y
    offset = x*tan(angle_four)

    """计算画笔的坐标以及绘画的长度"""
    gigantic_star_x = -10*little_block-offset
    gigantic_star_y = 8*little_block-y
    gigantic_length = 2*offset

    flag = turtle.Turtle()
    flag.speed(10)

    #Aide = True #辅助线是否可见
    
    if Auxi:
    #'''
        for i in range(1,10):
            draw_test_line(flag,-width/2,height/2-i*little_block,width/2)
        draw_test_line(flag,-width/2,height/2-10*little_block,width)

        flag.setheading(-90)

        for i in range(1,15):
            draw_test_line(flag,-width/2+i*little_block,height/2,height/2)
        draw_test_line(flag,-width/2+15*little_block,height/2,height)

        flag.setheading(0)
    #'''

    # 绘制大星
    draw_star(flag, gigantic_star_x, gigantic_star_y, gigantic_length, "yellow")
    
    if Auxi:
        draw_circle(flag,-10*little_block,2*little_block,3*little_block)

    #第一个小星星
    minor_star_1_x = -little_block*5-(little_block*(5/(34**(1/2))))
    minor_star_1_y = little_block*8-(little_block*(3/(34**(1/2))))
    degree1 = atan(3/5)
    flag.setheading(18+degrees(degree1))
    draw_star(flag, minor_star_1_x, minor_star_1_y, gigantic_length/3, "yellow")

    if Auxi:
        flag.setheading(0)
        draw_circle(flag,-5*little_block,7*little_block,little_block)
        draw_line(flag,-5*little_block,8*little_block,(34**(1/2))*little_block,180+degrees(atan(3/5)))
 
    #第二个小星星
    minor_star_2_x = -little_block*3-(little_block*(7/(50**(1/2))))
    minor_star_2_y = little_block*6-(little_block*(1/(50**(1/2))))
    degree2 = atan(1/7)
    flag.setheading(18+degrees(degree2))
    draw_star(flag, minor_star_2_x, minor_star_2_y, gigantic_length/3, "yellow")

    if Auxi:
        flag.setheading(0)
        draw_circle(flag,-3*little_block,5*little_block,little_block)
        draw_line(flag,-3*little_block,6*little_block,(50**(1/2))*little_block,180+degrees(atan(1/7)))

    #第三个小星星
    minor_star_3_x = -little_block*3-(little_block*(7/(53**(1/2))))
    minor_star_3_y = little_block*3+(little_block*(2/(53**(1/2))))
    degree3 = atan(2/7)
    flag.setheading(18-degrees(degree3))
    draw_star(flag, minor_star_3_x, minor_star_3_y, gigantic_length/3, "yellow")

    if Auxi:
        flag.setheading(0)
        draw_circle(flag,-3*little_block,2*little_block,little_block)
        draw_line(flag,-3*little_block,3*little_block,(53**(1/2))*little_block,180-degrees(atan(2/7)))

    #第四个小星星
    minor_star_4_x = -little_block*5-(little_block*(5/(41**(1/2))))
    minor_star_4_y = little_block*1+(little_block*(4/(41**(1/2))))
    degree4 = atan(4/5)
    flag.setheading(18-degrees(degree4))
    draw_star(flag, minor_star_4_x, minor_star_4_y, gigantic_length/3, "yellow")

    if Auxi:
        flag.setheading(0)
        draw_circle(flag,-5*little_block,0,little_block)
        draw_line(flag,-5*little_block,little_block,(41**(1/2))*little_block,180-degrees(atan(4/5)))

    flag.hideturtle()
    screen.mainloop()

if __name__=="__main__":
    draw_china_flag(0)

代码中还添加了辅助线用以检验作画是否标准,只需要将主函数里的参数改为True即可看到辅助线,带有辅助线的结果如下所示:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值