python画国旗

您所在的位置:网站首页 苏联国旗长宽比例 python画国旗

python画国旗

2024-07-14 03:24| 来源: 网络整理| 查看: 265

# 生成一面五星红旗,修改width跟height可以,画不同规格的红旗

import tkinter as tkimport math

width = 660height = 440

window = tk.Tk()canvas = tk.Canvas(width=width, height=height)canvas.pack()window.title("五星红旗")

def get_coordinates(width,height,star_type=0): """根据传入的宽、高,返回一组坐标 star_type中的0表示返回的为大五角星的中心点坐标, 1-4分别为从上到下的4颗小五角星坐标 """ # 反回中心点坐标及半径 if star_type == 0: center_x = width/2 - (width/30*10) center_y = height/2 - (height/20*5) r = (width / 30) *3 return center_x, center_y, r elif star_type == 1: center_x = width/2 - (width/30*5) center_y = height/2 - (height/20*8) r = (width / 30) *1 return center_x, center_y, r elif star_type == 2: center_x = width/2 - (width/30*3) center_y = height/2 - (height/20*6) r = (width / 30) *1 return center_x, center_y, r elif star_type == 3: center_x = width/2 - (width/30*3) center_y = height/2 - (height/20*3) r = (width / 30) *1 return center_x, center_y, r elif star_type == 4: center_x = width/2 - (width/30*5) center_y = height/2 - (height/20*1) r = (width / 30) *1 return center_x, center_y, r

def draw_rectangle(width,height): """根据传入的长、宽画出旗面""" # 确定中心点坐标 x = width / 2 y = height / 2 # 画出红旗的矩形 canvas.create_rectangle(x-(width/2),y-(height/2),x+(width/2),y+(height/2),outline='red',fill='red')

draw_rectangle(width,height)

def draw_star(x,y,star_type=0): """根据传入的长、宽及返回五角星的5个点坐标""" # 确定中心点坐标及半径 tem = get_coordinates(x,y,star_type) center_x = tem[0] center_y = tem[1] r = tem[2]

points=[ # 左上点 (center_x-r*math.sin(2*math.pi/5), center_y-r*math.cos(2*math.pi/5)),

# 右上点 (center_x+r*math.sin(2*math.pi/5), center_y-r*math.cos(2*math.pi/5)),

# 左下点 (center_x-r*math.sin(math.pi/5), center_y+r*math.cos(math.pi/5)),

# 顶点 (center_x, center_y-r),

# 右下点 (center_x+r*math.sin(math.pi/5), center_y+r*math.cos(math.pi/5)), ] #返回5个点的坐标组列表,中心点x、y坐标值及半径 return points,center_x,center_y,r

def rotate_point(points, center_x, center_y, angle): """对一组点进行旋转换算""" vertices = [] #对小五角星进行旋转 angle_rad = math.radians(angle) cos_theta = math.cos(angle_rad) sin_theta = math.sin(angle_rad) for point in points: rotated_x = center_x + (point[0] - center_x) * cos_theta - (point[1] - center_y) * sin_theta rotated_y = center_y + (point[0] - center_x) * sin_theta + (point[1] - center_y) * cos_theta vertices.append((rotated_x,rotated_y))

return vertices

#画出大五角星star0 = draw_star(width,height,0)canvas.create_polygon(star0[0],outline='yellow',fill='yellow')#画出第一颗小五角星star1 = draw_star(width,height,1)canvas.create_polygon(rotate_point(star1[0],star1[1],star1[2],-48.96), outline='yellow',fill='yellow')#画出第二颗小五角星star2 = draw_star(width,height,2)canvas.create_polygon(rotate_point(star2[0],star2[1],star2[2],-26.13), outline='yellow',fill='yellow')#画出第三颗小五角星star3 = draw_star(width,height,3)canvas.create_polygon(rotate_point(star3[0],star3[1],star3[2],-2.06), outline='yellow',fill='yellow')#画出第四颗小五角星star4 = draw_star(width,height,4)canvas.create_polygon(rotate_point(star4[0],star4[1],star4[2],20.66), outline='yellow',fill='yellow')

window.mainloop()



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3