You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

47 lines
1.6 KiB

import math
import tkinter as tk
def hexagonal_fractal_tree(depth, length, angle, center, canvas):
"""
绘制六边形
:param depth:
:param length:
:param angle:
:param center:
:param canvas:
:return:
"""
if depth == 0:
return
end_x = center[0] + length * math.cos(math.radians(angle))
end_y = center[1] - length * math.sin(math.radians(angle))
canvas.create_line(center[0], center[1], end_x, end_y, fill='LightBlue4', width=2)
hexagon_points = []
for i in range(6):
hexagon_x = end_x + length * math.cos(math.radians(60 * i))
hexagon_y = end_y - length * math.sin(math.radians(60 * i))
hexagon_points.extend([hexagon_x, hexagon_y])
canvas.create_polygon(hexagon_points, outline='LightBlue4', fill='', width=2)
new_length = length * 0.7
new_depth = depth - 1
new_angle1 = angle + 30
new_angle2 = angle - 30
hexagonal_fractal_tree(new_depth, new_length, new_angle1, (end_x, end_y), canvas)
hexagonal_fractal_tree(new_depth, new_length, new_angle2, (end_x, end_y), canvas)
window = tk.Tk()
window.title("Hexagonal Fractal Tree")
canvas = tk.Canvas(window, width=400, height=400, bg='white')
canvas.pack()
# Set the initial parameters and start drawing the tree
initial_length = 100
initial_angle = 90 # Start with a vertical trunk
initial_center = (200, 400) # Adjust the center as needed
initial_depth = 5 # You can change the depth
hexagonal_fractal_tree(initial_depth, initial_length, initial_angle, initial_center, canvas)
# Start the tkinter main loop
window.mainloop()