Embedded_game/模块一/画图/004_正五角星形.py

80 lines
2.3 KiB
Python
Raw Normal View History

2025-01-02 12:48:11 +08:00
# -*- coding:utf-8 -*-
# @Author len
# @Create 2023/12/31 11:18
import matplotlib.pyplot as plt
import numpy as np
def draw_star(color='blue'):
# 定义五角星的顶点
def star_point(angle, distance, center=(0, 0)):
return center[0] + distance * np.cos(angle), center[1] + distance * np.sin(angle)
# 创建星形的顶点坐标
points = []
for i in range(5):
points.append(star_point(i * 2 * np.pi / 5, 1))
points.append(star_point(i * 2 * np.pi / 5 + np.pi / 5, 0.5))
# 创建图形和轴,设置背景为透明
fig, ax = plt.subplots(figsize=(6, 6), dpi=100)
fig.patch.set_alpha(0.0)
# 绘制五角星,颜色为指定颜色
star = plt.Polygon(points, closed=True, color=color)
ax.add_patch(star)
# 设置坐标轴的范围和比例
ax.set_xlim(-1.5, 1.5)
ax.set_ylim(-1.5, 1.5)
ax.set_aspect('equal')
# 移除坐标轴
ax.axis('off')
# 保存图像为PNG格式背景设为透明
plt.savefig('star.png', transparent=True, bbox_inches='tight', pad_inches=0)
# 显示图像
plt.show()
# 调用函数并传入您选择的颜色
draw_star(color='blue') # 您可以替换为任何喜欢的颜色
# import matplotlib.pyplot as plt
# import numpy as np
#
# def draw_filled_five_pointed_star(ax, center=(0, 0), size=1):
# # 定义五芒星顶点的函数
# def star_point(angle, distance):
# return center[0] + distance * np.cos(angle), center[1] + distance * np.sin(angle)
#
# # 创建五个顶点
# points = [star_point(i * 2 * np.pi / 5, size) for i in range(5)]
# # 按照五芒星的顺序连接顶点
# star_path = [points[i % 5] for i in [0, 2, 4, 1, 3, 0]]
#
# # 分解出x和y坐标
# star_x, star_y = zip(*star_path)
#
# # 使用红色填充五芒星
# ax.fill(star_x, star_y, 'r')
#
# # 创建图形和轴,设置透明背景
# fig, ax = plt.subplots(figsize=(6, 6), dpi=100)
# fig.patch.set_alpha(0.0)
# ax.patch.set_alpha(0.0)
#
# # 绘制填充的五芒星
# draw_filled_five_pointed_star(ax)
#
# # 设置图形的比例相等,并移除坐标轴
# ax.set_aspect('equal')
# ax.axis('off')
#
# # 保存图像为PNG格式背景透明
# plt.savefig('filled_five_pointed_star.png', transparent=True, bbox_inches='tight', pad_inches=0)
#
# # 显示图形
# plt.show()