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.

42 lines
1.5 KiB

11 months ago
def Rect_Pattern():
canvas.delete("all") # 清空画布
canvas.create_rectangle(25, 30, 145, 140, outline='red', width=2)
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="#d8e6f7", 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) # 画右枝
canvas.update()
sleep(0.001)
分形层数:finishLevel = 8
左下点:leftPoint = (450, 510)
右下点:rightPoint = (550, 510)
初始角:initAngle = 45