Compare commits
5 Commits
Author | SHA1 | Date | |
---|---|---|---|
f0e02fcd45 | |||
![]() |
8c91c4d3f8 | ||
![]() |
d40959fe66 | ||
![]() |
7a729b728d | ||
![]() |
af62c1eed7 |
27
README.md
27
README.md
@ -1,11 +1,24 @@
|
||||
# Random-Bessel-Closed-Graph
|
||||
## 随机生成任意的封闭图形 | Randomly generate arbitrary closed graphs
|
||||
Python 7 及以上
|
||||
只有主函数,封装没有找到合适的exe导出
|
||||
软件也没有修成英文,抱歉啊啊
|
||||
含点数量最大值-最小值,粗细调整,
|
||||
Python 7 and up
|
||||
Only the main function is available, but the package is not exported to a suitable exe.
|
||||
The software has not been modified into English, sorry ah ah ah!
|
||||
Contains the maximum-minimum value of the number of points, the thickness of the adjustment.
|
||||
只有主函数
|
||||
含点数量最大值-最小值,粗细调整
|
||||
|
||||
|
||||
|
||||

|
||||
保存在本地图像例子
|
||||
Save as local image example
|
||||

|
||||
|
||||
### MIT
|
||||
|
||||
更新:
|
||||
|
||||
+-按钮调整
|
||||
|
||||
在线调整粗细实时更新
|
||||
|
||||
重新压缩了体积
|
||||
|
||||
从之前的300M压缩到70M
|
||||
|
108
work.py
108
work.py
@ -3,10 +3,21 @@ from tkinter import filedialog, messagebox
|
||||
import matplotlib.pyplot as plt
|
||||
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
|
||||
import numpy as np
|
||||
from scipy.interpolate import make_interp_spline, BSpline
|
||||
from scipy.interpolate import make_interp_spline
|
||||
from PIL import Image
|
||||
import io
|
||||
|
||||
# 更新输入框值的函数,支持整型和浮点型
|
||||
def update_entry_value(entry, delta, is_integer=False):
|
||||
try:
|
||||
value = float(entry.get()) + delta
|
||||
if is_integer:
|
||||
value = int(round(value)) # 确保为整数
|
||||
entry.delete(0, tk.END)
|
||||
entry.insert(0, str(value))
|
||||
except ValueError:
|
||||
pass
|
||||
|
||||
def generate_and_display_shape():
|
||||
try:
|
||||
min_vertices = int(min_entry.get())
|
||||
@ -18,28 +29,19 @@ def generate_and_display_shape():
|
||||
messagebox.showerror("无效输入", "最小和最大顶点数必须至少为3,并且最小值不应超过最大值。")
|
||||
return
|
||||
|
||||
# 清除之前的图形
|
||||
ax.clear()
|
||||
|
||||
# 生成随机顶点数量
|
||||
num_vertices = np.random.randint(min_vertices, max_vertices + 1)
|
||||
|
||||
# 生成随机角度
|
||||
angles = np.sort(np.random.rand(num_vertices) * 2 * np.pi)
|
||||
|
||||
# 添加散布
|
||||
angles += np.random.normal(0, scatter, num_vertices)
|
||||
angles = np.sort(angles % (2 * np.pi))
|
||||
|
||||
# 计算每个顶点的坐标
|
||||
x = np.cos(angles)
|
||||
y = np.sin(angles)
|
||||
|
||||
# 将第一个顶点添加到最后以闭合图形
|
||||
x = np.append(x, x[0])
|
||||
y = np.append(y, y[0])
|
||||
|
||||
# 使用B样条插值平滑曲线
|
||||
t = np.linspace(0, 1, num_vertices + 1)
|
||||
t_smooth = np.linspace(0, 1, 300)
|
||||
|
||||
@ -49,13 +51,12 @@ def generate_and_display_shape():
|
||||
x_smooth = spl_x(t_smooth)
|
||||
y_smooth = spl_y(t_smooth)
|
||||
|
||||
# 创建图形
|
||||
global current_line
|
||||
if current_line:
|
||||
current_line.remove()
|
||||
current_line, = ax.plot(x_smooth, y_smooth, linestyle='-', color='gray', linewidth=line_width) # 显示轨迹
|
||||
ax.axis('equal') # 确保比例相等
|
||||
ax.axis('off') # 隐藏坐标轴
|
||||
current_line, = ax.plot(x_smooth, y_smooth, linestyle='-', color='gray', linewidth=line_width)
|
||||
ax.axis('equal')
|
||||
ax.axis('off')
|
||||
canvas.draw()
|
||||
except ValueError:
|
||||
messagebox.showerror("无效输入", "请输入有效的整数作为最小和最大顶点数,以及浮点数作为线的粗细和散布。")
|
||||
@ -70,13 +71,10 @@ def adjust_line_width():
|
||||
messagebox.showerror("无效输入", "请输入有效的浮点数作为线的粗细。")
|
||||
|
||||
def save_shape():
|
||||
# 获取当前图形
|
||||
buf = io.BytesIO()
|
||||
plt.savefig(buf, format='png', transparent=True)
|
||||
buf.seek(0)
|
||||
image = Image.open(buf).convert("RGBA")
|
||||
|
||||
# 保存图像
|
||||
file_path = filedialog.asksaveasfilename(defaultextension=".png", filetypes=[("PNG 文件", "*.png"), ("所有文件", "*.*")])
|
||||
if file_path:
|
||||
image.save(file_path)
|
||||
@ -95,47 +93,79 @@ canvas_widget.pack(side=tk.TOP, fill=tk.BOTH, expand=True)
|
||||
|
||||
# 创建输入框和按钮的框架
|
||||
input_frame = tk.Frame(root)
|
||||
input_frame.pack(side=tk.TOP, padx=20, pady=20)
|
||||
input_frame.pack(side=tk.TOP, padx=30, pady=30)
|
||||
|
||||
# 线的粗细输入框和标签
|
||||
# 线的粗细控制
|
||||
line_width_label = tk.Label(input_frame, text="线的粗细:")
|
||||
line_width_label.grid(row=0, column=0, padx=5, pady=5)
|
||||
line_width_entry = tk.Entry(input_frame)
|
||||
line_width_label.grid(row=0, column=0, padx=10, pady=10, sticky="e")
|
||||
line_width_entry = tk.Entry(input_frame, width=8)
|
||||
line_width_entry.insert(0, "1.0")
|
||||
line_width_entry.grid(row=0, column=1, padx=5, pady=5)
|
||||
line_width_entry.grid(row=0, column=1, padx=10, pady=10)
|
||||
|
||||
# 散布输入框和标签
|
||||
# 线的粗细 +/- 按钮(只调整线宽)
|
||||
tk.Button(input_frame, text="-", width=3,
|
||||
command=lambda: [update_entry_value(line_width_entry, -0.1), adjust_line_width()]
|
||||
).grid(row=0, column=0, sticky="w")
|
||||
tk.Button(input_frame, text="+", width=3,
|
||||
command=lambda: [update_entry_value(line_width_entry, 0.1), adjust_line_width()]
|
||||
).grid(row=0, column=1, sticky="e")
|
||||
|
||||
# 散布控制
|
||||
scatter_label = tk.Label(input_frame, text="散布:")
|
||||
scatter_label.grid(row=0, column=2, padx=5, pady=5)
|
||||
scatter_entry = tk.Entry(input_frame)
|
||||
scatter_label.grid(row=0, column=2, padx=10, pady=10, sticky="e")
|
||||
scatter_entry = tk.Entry(input_frame, width=8)
|
||||
scatter_entry.insert(0, "0.1")
|
||||
scatter_entry.grid(row=0, column=3, padx=5, pady=5)
|
||||
scatter_entry.grid(row=0, column=3, padx=10, pady=10)
|
||||
|
||||
# 散布 +/- 按钮(刷新图形)
|
||||
tk.Button(input_frame, text="-", width=3,
|
||||
command=lambda: [update_entry_value(scatter_entry, -0.01), generate_and_display_shape()]
|
||||
).grid(row=0, column=2, sticky="w")
|
||||
tk.Button(input_frame, text="+", width=3,
|
||||
command=lambda: [update_entry_value(scatter_entry, 0.01), generate_and_display_shape()]
|
||||
).grid(row=0, column=3, sticky="e")
|
||||
|
||||
# 调整按钮
|
||||
adjust_button = tk.Button(input_frame, text="调整", command=adjust_line_width, width=10, height=2)
|
||||
adjust_button.grid(row=0, column=4, padx=5, pady=5)
|
||||
adjust_button.grid(row=0, column=4, padx=10, pady=10)
|
||||
|
||||
# 最小顶点数输入框和标签
|
||||
min_label = tk.Label(input_frame, text="最小顶点数 (>3):")
|
||||
min_label.grid(row=1, column=0, padx=5, pady=5)
|
||||
min_entry = tk.Entry(input_frame)
|
||||
# 最小顶点数控制
|
||||
min_label = tk.Label(input_frame, text=" 最小顶点数 (>3):")
|
||||
min_label.grid(row=1, column=0, padx=10, pady=10, sticky="e")
|
||||
min_entry = tk.Entry(input_frame, width=8)
|
||||
min_entry.insert(0, "10")
|
||||
min_entry.grid(row=1, column=1, padx=5, pady=5)
|
||||
min_entry.grid(row=1, column=1, padx=10, pady=10)
|
||||
|
||||
# 最大顶点数输入框和标签
|
||||
max_label = tk.Label(input_frame, text="最大顶点数:")
|
||||
max_label.grid(row=1, column=2, padx=5, pady=5)
|
||||
max_entry = tk.Entry(input_frame)
|
||||
# 最小顶点数 +/- 按钮(刷新图形)
|
||||
tk.Button(input_frame, text="-", width=3,
|
||||
command=lambda: [update_entry_value(min_entry, -1, is_integer=True), generate_and_display_shape()]
|
||||
).grid(row=1, column=0, sticky="w")
|
||||
tk.Button(input_frame, text="+", width=3,
|
||||
command=lambda: [update_entry_value(min_entry, 1, is_integer=True), generate_and_display_shape()]
|
||||
).grid(row=1, column=1, sticky="e")
|
||||
|
||||
# 最大顶点数控制
|
||||
max_label = tk.Label(input_frame, text=" 最大顶点数:")
|
||||
max_label.grid(row=1, column=2, padx=10, pady=10, sticky="e")
|
||||
max_entry = tk.Entry(input_frame, width=8)
|
||||
max_entry.insert(0, "15")
|
||||
max_entry.grid(row=1, column=3, padx=5, pady=5)
|
||||
max_entry.grid(row=1, column=3, padx=10, pady=10)
|
||||
|
||||
# 最大顶点数 +/- 按钮(刷新图形)
|
||||
tk.Button(input_frame, text="-", width=3,
|
||||
command=lambda: [update_entry_value(max_entry, -1, is_integer=True), generate_and_display_shape()]
|
||||
).grid(row=1, column=2, sticky="w")
|
||||
tk.Button(input_frame, text="+", width=3,
|
||||
command=lambda: [update_entry_value(max_entry, 1, is_integer=True), generate_and_display_shape()]
|
||||
).grid(row=1, column=3, sticky="e")
|
||||
|
||||
# 刷新按钮
|
||||
refresh_button = tk.Button(input_frame, text="刷新", command=generate_and_display_shape, width=10, height=2)
|
||||
refresh_button.grid(row=1, column=4, padx=5, pady=5)
|
||||
refresh_button.grid(row=1, column=4, padx=10, pady=10)
|
||||
|
||||
# 保存按钮
|
||||
save_button = tk.Button(input_frame, text="保存", command=save_shape, width=10, height=2)
|
||||
save_button.grid(row=1, column=5, padx=5, pady=5)
|
||||
save_button.grid(row=1, column=5, padx=10, pady=10)
|
||||
|
||||
# 初始化图形
|
||||
current_line = None
|
||||
|
Loading…
x
Reference in New Issue
Block a user