
在Python中绘制标准国旗并不简单,我们采用的方法在数学上称为解析法。把整个国旗换成直角坐标系,中心坐标为(0,0)。每个小格边长20,则国旗左上角坐标为(-300,200)、国旗长600,高400。Turtle是小海龟绘图库,Math是数学库,要用到里面的三角函数和反三角函数,以及圆周率pi值。
Python是一种解释型、面向对象、动态数据类型的高级程序设计语言。 Python于1989年底发明,第一个公开发行版发行于1991年。
使用Python自己的绘图工具即可,turtle。
# encoding:utf-8# python3.6
import turtle
def draw_japan_flag():
# 日本国旗为“太阳旗”,呈长方形,长与宽之比为3∶2。 旗面为白色,正中有一轮红日。
# 红日半径
r = 150
# 画布尺寸(宽,高)
turtle.screensize(900, 600)
# 设置显示窗口像素
turtle.setup(width=900, height=600)
# 移动画笔起点
turtle.penup()
turtle.goto(0, -r)
turtle.pendown()
# 设置画笔属性
turtle.pensize(5)
turtle.pencolor("red")
turtle.fillcolor("red")
# 绘制速度,1~10个不同速度等级,小于1或者大于10立即绘制
turtle.speed(0)
# 开始绘制红日
turtle.begin_fill()
turtle.circle(r)
turtle.end_fill()
turtle.mainloop()
if __name__ == "__main__":
draw_japan_flag()
# python6.6import turtle
def test():
# 加纳共和国国旗呈长方形,长与宽之比为3∶2。
# 自上而下由红、黄、绿三个平行相等的横长方形组成,黄色部分中间是一颗黑色五角星。
flag_h = 300
flag_w = 450
star_h = flag_h/3
turtle.pensize(2)
turtle.speed(5)
turtle.hideturtle()
def draw_rectangle(color):
turtle.pencolor(color)
turtle.fillcolor(color)
turtle.pendown()
turtle.begin_fill()
turtle.forward(flag_w)
turtle.right(90)
turtle.forward(star_h)
turtle.right(90)
turtle.forward(flag_w)
turtle.right(90)
turtle.forward(star_h)
turtle.end_fill()
turtle.penup()
turtle.back(star_h)
turtle.right(90)
turtle.penup()
turtle.goto(-flag_w / 2, flag_h / 2)
draw_rectangle("red")
draw_rectangle("yellow")
draw_rectangle("green")
# 五角星
turtle.penup()
turtle.goto(0, star_h/2)
turtle.pencolor("black")
turtle.fillcolor("black")
turtle.right(90-18)
turtle.pendown()
turtle.begin_fill()
for i in range(5):
turtle.forward(star_h)
turtle.right(180-36)
turtle.end_fill()
turtle.done()
if __name__ == "__main__":
test()
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)