热烈庆祝中华人民共和国建国70周年
严格按照国旗规格制作
"""
项目:五星红旗
方案:
严格按照国旗规格制作,有辅助线可选择显示
五星之位置与画法如下:
(1)为便于确定五星之位置,先将旗面对分为四个相等的长方形,将左上方之长方形上下划为十等分,左右划为十五等分。
(2)大五角星的中心点,在该长方形上五下五、左五右十之处。
其画法为:以此点为圆心,以三等分为半径作一圆。在此圆周上,定出五个等距离的点,其一点须位于圆之正上方。
然后将此五点中各相隔的两点相联,使各成一直线。此五直线所构成之外轮廓线,即为所需之大五角星。五角星之一个角尖正向上方。
(3)四颗小五角星的中心点,第一点在该长方形上二下八、左十右五之处,
第二点在上四下六、左十二右三之处,
第三点在上七下三、左十二右三之处,
第四点在上九下一、左十右五之处。
其画法为:以以上四点为圆心,各以一等分为半径,分别作四个圆。
在每个圆上各定出五个等距离的点,其中均须各有一点位于大五角星中心点与以上四个圆心的各联结线上。
然后用构成大五角星的同样方法,构成小五角星。此四颗小五角星均各有一个角尖正对大五角星的中心点。
颜色:
标准:RGB
红色:222, 41, 16
黄色:255, 222, 0
"""
import turtle as tl
import math
k = 600
ch = 3 / 2 * k
s_k = k / 20 # 左上旗面小方格宽度
tl.screensize(canvwidth=ch, canvheight=k, bg=None)
tl.colormode(255)
tl.speed(12)
flag_red = 222, 41, 16
flag_yellow = 255, 222, 0
# 各星中心点位置
zh_d = [-10 * s_k, 5 * s_k]
zh_1 = [-5 * s_k, 8 * s_k]
zh_2 = [-3 * s_k, 6 * s_k]
zh_3 = [-3 * s_k, 3 * s_k]
zh_4 = [-5 * s_k, s_k]
# 设置画笔起始点在画布左上顶点
tl.penup()
tl.goto(-ch / 2, k / 2)
tl.pendown()
# 画红色旗面
tl.pencolor(flag_red)
tl.fillcolor(flag_red)
tl.begin_fill()
for i in range(2):
tl.forward(ch)
tl.right(90)
tl.forward(k)
tl.right(90)
tl.end_fill()
# 定义星的画法
def star(x, y, r, d):
"""
:param x: 五角星所在圆圆心x坐标
:param y: 五角星所在圆圆心y坐标
:param r: 五角星所在圆半径
:param d: 初始右转角度
:return:
"""
c = math.cos(math.radians(54)) * r / math.cos(math.radians(36)) # 五角星小边长
tl.setheading(0) # 恢复默认朝向:右
tl.penup()
tl.goto(x, y)
tl.right(d)
tl.fd(r)
tl.right(162)
tl.pendown()
tl.begin_fill()
for i in range(5):
tl.fd(c)
tl.right(-72)
tl.fd(c)
tl.right(144)
tl.end_fill()
# 大星
tl.color(flag_yellow, flag_yellow)
star(zh_d[0], zh_d[1], 3 * s_k, 270)
# 求小星右转角度
def zhuanjiao(a, b):
"""
:param a: 小星中心与大星中心横向距离多少方块
:param b: 小星中心与大星中心纵向距离多少方块
:return:
"""
d = math.degrees(math.atan(b / a))
return d
# 第一颗小星
d1 = zhuanjiao(5, 3)
star(zh_1[0], zh_1[1], s_k, 180 - d1)
# 第二颗小星
d2 = zhuanjiao(7, 1)
star(zh_2[0], zh_2[1], s_k, 180 - d2)
# 第三颗小星
d3 = zhuanjiao(7, 2)
star(zh_3[0], zh_3[1], s_k, 198)
# 第四颗小星
d4 = zhuanjiao(5, 4)
star(zh_4[0], zh_4[1], s_k, 180 + d4)
"""
检查画线是否准确
每30个像素画横线、竖线一条
颜色黑色
"""
def p_yuan(x, y, r):
"""
以五角星中心为圆心画圆
"""
tl.setheading(0)
tl.penup()
tl.goto(x, y)
tl.fd(r)
tl.setheading(-90)
tl.pendown()
tl.circle(-r)
def lianxian(x, y):
"""
大星与小星中心点之间的连线
"""
tl.penup()
tl.goto(zh_d[0], zh_d[1])
tl.pendown()
tl.goto(x, y)
dayin = 1 # 当值为1时画辅助线,不想画辅助线可改为其他值
if dayin == 1:
tl.pensize(2)
tl.pencolor('black')
# 中横线
tl.setheading(0)
tl.penup()
tl.goto(-ch / 2, 0)
tl.pendown()
tl.fd(ch)
# 中竖线
tl.setheading(-90)
tl.penup()
tl.goto(0, k / 2)
tl.pendown()
tl.fd(k)
# 在第一象限打30像素方格
# 横线
tl.setheading(0)
for i in range(10):
tl.penup()
tl.goto(-ch / 2, s_k * i)
tl.pendown()
tl.fd(ch / 2)
# 竖线
tl.setheading(-90)
for i in range(15):
tl.penup()
tl.goto(-s_k * i, k / 2)
tl.pendown()
tl.fd(k / 2)
# 画圆
# 大星圆
p_yuan(zh_d[0], zh_d[1], 3 * s_k)
# 第一小星圆
p_yuan(zh_1[0], zh_1[1], s_k)
# 第二小星圆
p_yuan(zh_2[0], zh_2[1], s_k)
# 第三小星圆
p_yuan(zh_3[0], zh_3[1], s_k)
# 第四小星圆
p_yuan(zh_4[0], zh_4[1], s_k)
# 画大星与小星中心之间连线
# 第一星
lianxian(zh_1[0], zh_1[1])
# 第二星
lianxian(zh_2[0], zh_2[1])
# 第三星
lianxian(zh_3[0], zh_3[1])
# 第四星
lianxian(zh_4[0], zh_4[1])
# 签名
tl.pencolor('yellow')
tl.penup()
tl.goto(ch/10, -k/3)
tl.pendown()
tl.write('Made by QianHua-209', font=("微软雅黑", 22))
# 隐藏画笔
tl.hideturtle()
tl.done()
辅助线检验
以下为最初版本,留个记录
"""
重新学习python,100天
第一天:2019年11月2日
项目:使用turtle画一幅海岛日出图,岛上有椰树,日出海面有倒影,蓝色天空有白云,全部使用横线
先画个国旗练练手
"""
import turtle as tl
# 切换RGB颜色模式
tl.colormode(255)
# 设置画笔速度
tl.speed(12)
# 设置画布大小
tl.setup(width=900, height=600, startx=None, starty=None)
# 设置画笔起始点在画布左上顶点
tl.penup()
tl.goto(-450, 300)
tl.pendown()
# 画一个800×400的旗面
tl.pencolor('red')
tl.fillcolor('red')
tl.begin_fill()
tl.forward(900)
tl.right(90)
tl.forward(600)
tl.right(90)
tl.forward(900)
tl.right(90)
tl.forward(600)
tl.end_fill()
# 大星
tl.penup()
tl.goto(-390, 180)
tl.right(90)
tl.pendown()
tl.pencolor('yellow')
tl.fillcolor('yellow')
tl.begin_fill()
for i in range(5):
tl.fd(180)
tl.right(144)
tl.end_fill()
# 第一颗小星
tl.penup()
tl.goto(-120, 240)
tl.left(198)
tl.pendown()
tl.pencolor('yellow')
tl.fillcolor('yellow')
tl.begin_fill()
for i in range(5):
tl.fd(60)
tl.right(144)
tl.end_fill()
# 第二颗小星
tl.setheading(0)
tl.penup()
tl.goto(-90, 180)
tl.left(188)
tl.fd(30)
tl.right(162)
tl.pendown()
tl.pencolor('yellow')
tl.fillcolor('yellow')
tl.begin_fill()
for i in range(5):
tl.fd(60)
tl.right(144)
tl.end_fill()
# 第三颗小星
tl.setheading(0)
tl.penup()
tl.goto(-90, 90)
tl.left(90)
tl.fd(30)
tl.right(162)
tl.pendown()
tl.pencolor('yellow')
tl.fillcolor('yellow')
tl.begin_fill()
for i in range(5):
tl.fd(60)
tl.right(144)
tl.end_fill()
# 第四颗小星
tl.setheading(0)
tl.penup()
tl.goto(-150, 30)
tl.fd(30)
tl.right(162)
tl.pendown()
tl.pencolor('yellow')
tl.fillcolor('yellow')
tl.begin_fill()
for i in range(5):
tl.fd(60)
tl.right(144)
tl.end_fill()
"""
检查画线是否准确
每30个像素画横线、竖线一条
颜色黑色
"""
# 中横线
tl.setheading(0)
tl.pencolor('black')
tl.pensize(2)
tl.penup()
tl.goto(-450, 0)
tl.pendown()
tl.fd(900)
# 中竖线
tl.setheading(-90)
tl.penup()
tl.goto(0, 300)
tl.pendown()
tl.fd(600)
# 在第一象限打30像素方格
# 横线
tl.setheading(0)
for i in range(10):
tl.penup()
tl.goto(-450, 30 * i)
tl.pendown()
tl.fd(450)
# 竖线
tl.setheading(-90)
for i in range(15):
tl.penup()
tl.goto(-30 * i, 300)
tl.pendown()
tl.fd(300)
# 画圆
# 大星圆
tl.penup()
tl.goto(-210, 150)
tl.pendown()
tl.circle(-90)
# 第一小星圆
tl.penup()
tl.goto(-120, 240)
tl.pendown()
tl.circle(-30)
# 第二小星圆
tl.penup()
tl.goto(-60, 180)
tl.pendown()
tl.circle(-30)
# 第三小星圆
tl.penup()
tl.goto(-60, 90)
tl.pendown()
tl.circle(-30)
# 第四小星圆
tl.penup()
tl.goto(-120, 30)
tl.pendown()
tl.circle(-30)
# 画大星与小星中心之间连线
# 第一星
tl.penup()
tl.goto(-300, 150)
tl.pendown()
tl.goto(-150, 240)
# 第二星
tl.penup()
tl.goto(-300, 150)
tl.pendown()
tl.goto(-90, 180)
# 第三星
tl.penup()
tl.goto(-300, 150)
tl.pendown()
tl.goto(-90, 90)
# 第四星
tl.penup()
tl.goto(-300, 150)
tl.pendown()
tl.goto(-150, 30)
# 签名
tl.pencolor('yellow')
tl.penup()
tl.goto(100, -220)
tl.pendown()
tl.write('Made by QianHua-209', font=("微软雅黑", 22))
# 隐藏画笔
tl.hideturtle()
tl.done()
"""
仍有改进余地
第一,五角星与规定大小有出入,应进一步细化,或者换一种算法
第二,重复动作可以函数化
"""