# FH的python代码 # 文本分析和图像识别 # 开发时间 2023/7/17 16:40 import math import fractaltree def run_Y_tree(): # Y形分形树函数 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) # canvas.update() # canvas.after(1) 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) # 画左枝 # print(start_point,end_point) # 清除画布 cv_big.delete("all") # 将初始参数传入函数 Y_tree(finishLevel_Y, treePercent_Y, leftAngle_Y, rightAngle_Y, initPoint_Y, initAngle_Y, initLong_Y, cv_big) output_text.delete("1.0", "end") # 在底部的结果输出框输出分形树的绘制参数 text = " 结果输出:<分形绘制参数的字符串形式输出>\n finishLevel={}, treePercent={}, leftAngle={}, rightAngle={}, " \ "initPoint={}, initAngle={}, initLong={}".format(finishLevel_Y, treePercent_Y, leftAngle_Y, rightAngle_Y, initPoint_Y, initAngle_Y, initLong_Y) output_text.insert(tk.INSERT, text) # 三角形分形树执行函数(倒的坐标系) def run_Triangle_tree(): # 三角形分形树函数 def Triangle_tree(depth, percent, start_point, angle, length, canvas): if depth == 0: return # 左枝点 end_point1 = (start_point[0] - length * math.sin(math.radians(angle)), start_point[1] - length * math.cos(math.radians(angle))) # 右枝点 end_point2 = (start_point[0] + length * math.sin(math.radians(angle)), start_point[1] - length * math.cos(math.radians(angle))) # 画三角形 canvas.create_polygon(start_point, end_point1, end_point2, fill='LightBlue4', outline='white') # 画左枝 Triangle_tree(depth - 1, percent, end_point1, angle, length * percent, canvas) # 画右枝 Triangle_tree(depth - 1, percent, end_point2, angle, length * percent, canvas) # canvas.update() # canvas.after(10) # 方形分形树执行函数 def run_Rect_tree(): # 方形分形树函数 def Rect_tree(depth, point1, point2, angle, canvas): # 直线的旋转,point1是定点 def rotate(point1, point2, angle): x1, y1 = point1[0], point1[1] x2, y2 = point2[0], point2[1] x = x1 + (x2 - x1) * math.cos(math.radians(angle)) + (y2 - y1) * math.sin(math.radians(angle)) y = y1 + (y2 - y1) * math.cos(math.radians(angle)) - (x2 - x1) * math.sin(math.radians(angle)) point = (x, y) # print(point) return point # 直线的缩放,point1是定点 def zoom(point1, point2, ratio): x1, y1 = point1[0], point1[1] x2, y2 = point2[0], point2[1] x = x1 + (x2 - x1) * ratio y = y1 + (y2 - y1) * ratio point = (x, y) return point point3 = rotate(point1, point2, 90) point4 = rotate(point2, point1, 270) # print(point1,point2,point3,point4) # 画正方形 canvas.create_polygon(point1, point2, point4, point3, fill='LightBlue4', outline='white') if depth == 0: return point = rotate(point3, point4, angle) # 旋转 point = zoom(point3, point, math.cos(math.radians(angle))) # 缩放 Rect_tree(depth - 1, point, point4, angle, canvas) # 画左枝 Rect_tree(depth - 1, point3, point, angle, canvas) # 画右枝