我們國傢什麼最有代表性,請看python畫出的五星紅旗
當我們還是小學生開始,我們上學時就開始要經常有升國旗的儀式,這種儀式一直持續到我退伍,參加工作後就沒有經常參加這種儀式了,但是我們國家的國旗代表了我們的不斷髮展,邁向全球的有力象徵。
五星紅旗(python)
程式分享如下:
import turtle
import math
def draw_polygon(aTurtle, size=50, n=3):
‘’‘
繪製正多邊形
args:
aTurtle: turtle物件例項
size: int型別,正多邊形的邊長
n: int型別,是幾邊形
’‘’
for i in range(n):
aTurtle。forward(size)
aTurtle。left(360。0/n)
def draw_n_angle(aTurtle, size=50, num=5, color=None):
‘’‘
繪製正n角形,預設為黃色
args:
aTurtle: turtle物件例項
size: int型別,正多角形的邊長
n: int型別,是幾角形
color: str,圖形顏色,預設不填色
’‘’
if color:
aTurtle。begin_fill()
aTurtle。fillcolor(color)
for i in range(num):
aTurtle。forward(size)
aTurtle。left(360。0/num)
aTurtle。forward(size)
aTurtle。right(2*360。0/num)
if color:
aTurtle。end_fill()
def draw_5_angle(aTurtle=None, start_pos=(0,0), end_pos=(0,10), radius=100, color=None):
‘’‘
根據起始位置、結束位置和外接圓半徑畫五角星
args:
aTurtle: turtle物件例項
start_pos: int的二元tuple,要畫的五角星的外接圓圓心
end_pos: int的二元tuple,圓心指向的位置座標點
radius: 五角星外接圓半徑
color: str, 圖形顏色,預設不填色
’‘’
aTurtle = aTurtle or turtle。Turtle()
size = radius * math。sin(math。pi/5)/math。sin(math。pi*2/5)
aTurtle。left(math。degrees(math。atan2(end_pos[1]-start_pos[1], end_pos[0]-start_pos[0])))
aTurtle。penup()
aTurtle。goto(start_pos)
aTurtle。fd(radius)
aTurtle。pendown()
aTurtle。right(math。degrees(math。pi*9/10))
draw_n_angle(aTurtle, size, 5, color)
def draw_5_star_flag(times=20。0):
‘’‘
繪製五星紅旗
args:
times: 五星紅旗的規格為30*20, times為倍數,預設大小為10倍, 即300*200
’‘’
width, height = 30*times, 20*times # 初始化螢幕和海龜
window = turtle。Screen()
aTurtle = turtle。Turtle()
aTurtle。hideturtle()
aTurtle。speed(10) # 畫紅旗
aTurtle。penup()
aTurtle。goto(-width/2, height/2)
aTurtle。pendown()
aTurtle。begin_fill()
aTurtle。fillcolor(‘red’)
aTurtle。fd(width)
aTurtle。right(90)
aTurtle。fd(height)
aTurtle。right(90)
aTurtle。fd(width)
aTurtle。right(90)
aTurtle。fd(height)
aTurtle。right(90)
aTurtle。end_fill() # 畫大星星
draw_5_angle(aTurtle, start_pos=(-10*times, 5*times), end_pos=(-10*times, 8*times), radius=3*times, color=‘yellow’) # 畫四個小星星
stars_start_pos = [(-5, 8), (-3, 6), (-3, 3), (-5, 1)]
for pos in stars_start_pos:
draw_5_angle(aTurtle, start_pos=(pos[0]*times, pos[1]*times), end_pos=(-10*times, 5*times), radius=1*times, color=‘yellow’) # 點選關閉視窗
window。exitonclick()
if __name__ == ‘__main__’:
draw_5_star_flag()
python3數學模組用法:
import math
# 或
from math import *
變數 描述
math。e 自然對數的底e
math。pi 圓周率pi
函式名 描述
math。ceil(x) 對x向上取整,比如x=1。2,返回2
math。floor(x) 對x向下取整,比如x=1。2,返回1
math。sqrt(x) 返回x的平方根
math。factorial(x) 求x的階乘
math。log(x[, base]) 返回以base為底x的對數, 如果不給出base,則以自然對數e為底
math。log10(x) 求以10為底x的對數
math。pow(x, y) 返回 x**y (x的y次方)
math。fabs(x) 返回浮點數x的絕對值
角度和弧度degrees互換
math。degree(x) 將弧度x轉換為角度
math。radians(x) 將角度x轉換為弧度
三角函式
math。sin(x) 返回x的正弦(x為弧度)
math。cos(x) 返回x的餘弦(x為弧度)
math。tan(x) 返回x的正切(x為弧度)
math。asin(x) 返回x的反正弦(返回值為為弧度)
math。acos(x) 返回x的反餘弦(返回值為為弧度)
math。atan(x) 返回x的反正切(返回值為為弧度)