随机化每个递归的颜色
Randomizing colours of each recursion
我这里有一个工作的分形 H 树,它根据整棵树的颜色随机化颜色。
但是,我想尝试让每个单独的分形(每个递归)成为随机颜色。我不确定该怎么做,并尝试寻找解决方案,但都没有成功。
我对其中的一些东西有点陌生,如果这是一个愚蠢的问题,我很抱歉
import turtle
import time
import random
SPEED = 10
BG_COLOR = "white"
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 800
DRAWING_WIDTH = 700
DRAWING_HEIGHT = 700
PEN_WIDTH = 4
TITLE = "Fractal H tree"
FRACTAL_DEPTH = 3
color = [random.randint(0,255), random.randint(0,255), random.randint(0,255)]
def draw_line(tur, pos1, pos2):
# print("Drawing from", pos1, "to", pos2)
tur.penup()
tur.goto(pos1[0], pos1[1])
tur.pendown()
tur.goto(pos2[0], pos2[1])
def recursive_draw(tur, x, y, width, height, count):
time.sleep(0.5)
draw_line(tur, [x + width * 0.25, height // 2 + y],[x + width * 0.75, height // 2 + y],)
draw_line(tur,[x + width * 0.25, (height * 0.5) // 2 + y],[x + width * 0.25, (height * 1.5) // 2 + y],)
draw_line(tur,[x + width * 0.75, (height * 0.5) // 2 + y],[x + width * 0.75, (height * 1.5) // 2 + y],)
if count <= 0:
return
else: # The recursive part
count -= 1
# Top left
recursive_draw(tur, x, y, width // 2, height // 2, count)
# Top right
recursive_draw(tur, x + width // 2, y, width // 2, height // 2, count)
# Bottom left
recursive_draw(tur, x, y + width // 2, width // 2, height // 2, count)
# Bottom right
recursive_draw(tur, x + width // 2, y + width // 2, width // 2, height // 2, count)
if __name__ == "__main__":
# Screen setup
screen = turtle.Screen()
screen.setup(SCREEN_WIDTH, SCREEN_HEIGHT)
screen.title(TITLE)
screen.bgcolor(BG_COLOR)
screen.colormode(255)
# Turtle (pen) setup
tur = turtle.Turtle()
tur.hideturtle()
tur.pencolor(color)
tur.pensize(PEN_WIDTH)
tur.speed(SPEED)
# Initial call to recursive draw function
recursive_draw(tur, - DRAWING_WIDTH / 2, - DRAWING_HEIGHT / 2, DRAWING_WIDTH, DRAWING_HEIGHT, FRACTAL_DEPTH)
turtle.done()
就像 Samwise 所说的那样,只需在递归绘图中设置笔的颜色即可。
我这里有一个工作的分形 H 树,它根据整棵树的颜色随机化颜色。
但是,我想尝试让每个单独的分形(每个递归)成为随机颜色。我不确定该怎么做,并尝试寻找解决方案,但都没有成功。
我对其中的一些东西有点陌生,如果这是一个愚蠢的问题,我很抱歉
import turtle
import time
import random
SPEED = 10
BG_COLOR = "white"
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 800
DRAWING_WIDTH = 700
DRAWING_HEIGHT = 700
PEN_WIDTH = 4
TITLE = "Fractal H tree"
FRACTAL_DEPTH = 3
color = [random.randint(0,255), random.randint(0,255), random.randint(0,255)]
def draw_line(tur, pos1, pos2):
# print("Drawing from", pos1, "to", pos2)
tur.penup()
tur.goto(pos1[0], pos1[1])
tur.pendown()
tur.goto(pos2[0], pos2[1])
def recursive_draw(tur, x, y, width, height, count):
time.sleep(0.5)
draw_line(tur, [x + width * 0.25, height // 2 + y],[x + width * 0.75, height // 2 + y],)
draw_line(tur,[x + width * 0.25, (height * 0.5) // 2 + y],[x + width * 0.25, (height * 1.5) // 2 + y],)
draw_line(tur,[x + width * 0.75, (height * 0.5) // 2 + y],[x + width * 0.75, (height * 1.5) // 2 + y],)
if count <= 0:
return
else: # The recursive part
count -= 1
# Top left
recursive_draw(tur, x, y, width // 2, height // 2, count)
# Top right
recursive_draw(tur, x + width // 2, y, width // 2, height // 2, count)
# Bottom left
recursive_draw(tur, x, y + width // 2, width // 2, height // 2, count)
# Bottom right
recursive_draw(tur, x + width // 2, y + width // 2, width // 2, height // 2, count)
if __name__ == "__main__":
# Screen setup
screen = turtle.Screen()
screen.setup(SCREEN_WIDTH, SCREEN_HEIGHT)
screen.title(TITLE)
screen.bgcolor(BG_COLOR)
screen.colormode(255)
# Turtle (pen) setup
tur = turtle.Turtle()
tur.hideturtle()
tur.pencolor(color)
tur.pensize(PEN_WIDTH)
tur.speed(SPEED)
# Initial call to recursive draw function
recursive_draw(tur, - DRAWING_WIDTH / 2, - DRAWING_HEIGHT / 2, DRAWING_WIDTH, DRAWING_HEIGHT, FRACTAL_DEPTH)
turtle.done()
就像 Samwise 所说的那样,只需在递归绘图中设置笔的颜色即可。