|
|
|
|
import tkinter as tk
|
|
|
|
|
import tkinter.font as tf
|
|
|
|
|
import math
|
|
|
|
|
|
|
|
|
|
FinishLevel_Y = 14
|
|
|
|
|
TreePercent_Y = 0.7
|
|
|
|
|
LeftAngle_Y = 30
|
|
|
|
|
RightAngle_Y = 30
|
|
|
|
|
InitPoint_Y = (420, 500)
|
|
|
|
|
InitAngle_Y = 90
|
|
|
|
|
InitLong_Y = 150
|
|
|
|
|
|
|
|
|
|
params_Y = " 分形层数:finishLevel={}\n" \
|
|
|
|
|
" 缩放比例:treePercent={}\n" \
|
|
|
|
|
" 左枝倾角:leftAngle={}\n" \
|
|
|
|
|
" 右枝倾角:rightAngle={}\n" \
|
|
|
|
|
" 初始点:initPoint={}\n" \
|
|
|
|
|
" 初始角:initAngle={}\n" \
|
|
|
|
|
" 初始长度:initLong={}".format(FinishLevel_Y, TreePercent_Y, LeftAngle_Y, RightAngle_Y,
|
|
|
|
|
InitPoint_Y, InitAngle_Y, InitLong_Y)
|
|
|
|
|
|
|
|
|
|
# 创建全局变量 cv_small 和 cv_big
|
|
|
|
|
cv_small = None
|
|
|
|
|
cv_big = None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Y形基本图案函数
|
|
|
|
|
def yPattern():
|
|
|
|
|
cv_small.delete("all") # 清除画布
|
|
|
|
|
cv_small.create_line(30, 10, 90, 70, fill='red', width=2) # 左臂
|
|
|
|
|
cv_small.create_line(150, 10, 90, 70, fill='red', width=2) # 右臂
|
|
|
|
|
cv_small.create_line(90, 140, 90, 70, fill='red', width=2)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Y形树执行函数
|
|
|
|
|
def run_Y_tree():
|
|
|
|
|
def Y_tree(depth, percent, left_angle, right_angle, start_point, angle, length, canvas):
|
|
|
|
|
if depth == 0:
|
|
|
|
|
return
|
|
|
|
|
end_point = (start_point[0] + length * math.cos(math.radians(angle)),
|
|
|
|
|
start_point[1] - length * math.sin(math.radians(angle)))
|
|
|
|
|
canvas.create_line(start_point, end_point, fill='LightBlue4', width=2)
|
|
|
|
|
Y_tree(depth - 1, percent, left_angle, right_angle, end_point, angle + right_angle, length * percent,
|
|
|
|
|
canvas) # 画右枝
|
|
|
|
|
Y_tree(depth - 1, percent, left_angle, right_angle, end_point, angle - left_angle, length * percent,
|
|
|
|
|
canvas) # 画左枝
|
|
|
|
|
|
|
|
|
|
root = tk.Tk()
|
|
|
|
|
root.geometry('1270x650')
|
|
|
|
|
root.title('分形树')
|
|
|
|
|
|
|
|
|
|
ft3 = tf.Font(family="黑体", size=18)
|
|
|
|
|
|
|
|
|
|
global cv_small, cv_big # 声明 cv_small 和 cv_big 为全局变量
|
|
|
|
|
|
|
|
|
|
# 创建蓝色边框的大画布
|
|
|
|
|
cv_big = tk.Canvas(root, bg="white", highlightthickness=1, highlightbackground="LightBlue")
|
|
|
|
|
cv_big_width = 880
|
|
|
|
|
cv_big_height = 690
|
|
|
|
|
cv_big.config(width=cv_big_width, height=cv_big_height)
|
|
|
|
|
cv_big.place(x=330, y=10, anchor='nw')
|
|
|
|
|
|
|
|
|
|
# 创建红色边框的小画布
|
|
|
|
|
cv_small_width = cv_big_width // 4
|
|
|
|
|
cv_small_height = cv_big_height // 4
|
|
|
|
|
cv_small = tk.Canvas(root, bg="white", highlightthickness=1, highlightbackground="red")
|
|
|
|
|
cv_small.config(width=cv_small_width, height=cv_small_height)
|
|
|
|
|
cv_small.place(x=530, y=cv_big_height - cv_small_height + 10, anchor='nw')
|
|
|
|
|
|
|
|
|
|
# 绘制Y形基本图案
|
|
|
|
|
yPattern()
|
|
|
|
|
|
|
|
|
|
# 调用绘制Y形分形树的函数
|
|
|
|
|
Y_tree(FinishLevel_Y, TreePercent_Y, LeftAngle_Y, RightAngle_Y, InitPoint_Y, InitAngle_Y, InitLong_Y, cv_big)
|
|
|
|
|
|
|
|
|
|
# 创建底部的结果输出框
|
|
|
|
|
output_text = tk.Text(root, bg="white", highlightthickness=1, highlightbackground="LightBlue")
|
|
|
|
|
output_text.configure(font=ft3, spacing1=8)
|
|
|
|
|
output_text.insert(tk.INSERT, params_Y)
|
|
|
|
|
# 设置文本框不可编辑
|
|
|
|
|
output_text.configure(state="disabled")
|
|
|
|
|
output_text.place(x=20, y=800, width=1390, height=80, anchor='sw')
|
|
|
|
|
|
|
|
|
|
root.mainloop()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# 调用运行Y形树的函数
|
|
|
|
|
run_Y_tree()
|