Python画五星红旗

import matplotlib.pyplot as plt
import matplotlib.patches as patches
import math

def draw_star(ax,center_x,center_y,radius,color,rotation):
    degree=[72*i+rotation for i in range(5)]
    star_points=[(center_x+math.cos(math.radians(d))*radius,center_y+math.sin(math.radians(d))*radius) for d in degree]

    star_polygon=[
        star_points[0],star_points[2],
        star_points[4],star_points[1],
        star_points[3],star_points[0]
    ]

    ax.add_patch(patches.Polygon(star_polygon,closed=True,color=color))

unit=0.5
width=30*unit
height=20*unit

fig,ax=plt.subplots(figsize=(width,height))
ax.set_xlim(0,width)
ax.set_ylim(0,height)
ax.axis('off')

ax.add_patch(patches.Rectangle((0,0),width,height,linewidth=0,edgecolor='r',facecolor='r'))

big_star_center_height=15*unit
big_star_center_width=5*unit
big_star_radius=3*unit
draw_star(ax,big_star_center_width,big_star_center_height,big_star_radius,'yellow',90)

small_stars_centers=[(10*unit,18*unit),(12*unit,16*unit),(12*unit,13*unit),(10*unit,11*unit)]
small_star_radius=1*unit
for cx,cy in small_stars_centers:
    rotation=math.degrees(math.atan2(big_star_center_height-cy,big_star_center_width-cx))-72
    draw_star(ax,cx,cy,small_star_radius,'yellow',rotation)

plt.show()
### 使用Python绘制中国国旗 为了实现这一目标,可以采用`matplotlib`库来完成绘图工作。下面是一个详细的解决方案。 #### 导入必要的库 首先需要导入用于图形绘制的库[^1]。 ```python import matplotlib.pyplot as plt from matplotlib.patches import Polygon, Circle import numpy as np ``` #### 定义颜色和比例参数 定义绘制过程中需要用到的颜色以及尺寸比例等参数[^2]。 ```python red_color = '#DA291C' # 红旗的标准红色 yellow_color = '#FFD700' # 黄星的颜色 flag_width = 300 # 设置旗帜宽度 flag_height = int(flag_width * (2 / 3)) # 根据实际比例计算高度 star_size_large = flag_width * 0.18 # 大星星大小 star_size_small = star_size_large * 0.3 # 小星星相对大星星的比例 ``` #### 创建布并设置背景色 创建一个新的图像窗口,并将其填充为中国国旗的基础红色[^3]。 ```python fig, ax = plt.subplots(figsize=(6, 4)) ax.set_facecolor(red_color) plt.axis('off') # 关闭坐标轴显示 ``` #### 绘制五角星函数 编写一个辅助函数用来简化重复性的五边形绘制操作[^4]。 ```python def draw_star(center_x, center_y, size, rotation_angle=0): """Draw a five-pointed star at specified position with given size and angle.""" angles = np.linspace(0, 2*np.pi, 6)[:-1] + np.deg2rad(rotation_angle) points = [] for i in range(len(angles)): r = size if i % 2 == 0 else size/2.5 x = center_x + r * np.cos(angles[i]) y = center_y + r * np.sin(angles[i]) points.append((x,y)) polygon = Polygon(points, closed=True, fill=True, edgecolor='none', facecolor=yellow_color) ax.add_patch(polygon) # 主要的大五角星位于左上角位置 draw_star(flag_width*0.15, flag_height*0.3, star_size_large) # 较小的四颗副星围绕着主要的一颗分布 positions_of_smaller_stars = [ (0.26, 0.1), (0.4, 0.2), (0.4, 0.4), (0.26, 0.5) ] for pos in positions_of_smalls_stars: adjusted_pos = tuple(map(lambda v: v * flag_width, pos)) draw_star(*adjusted_pos, star_size_small, rotation_angle=-np.arctan(pos[1]/pos[0])*180/np.pi+90) ``` #### 展示最终效果 最后一步就是调用Matplotlib内置的方法展示所构建的面了[^5]。 ```python plt.show() ``` 通过上述代码片段即可成功模拟出具有代表意义的中华人民共和国国旗图案。需要注意的是,在正式场合下应当严格遵循官方发布的标准规格进行制作;这里仅提供一种近似的视觉呈现方式供学习交流之用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值