我想在python中创建一个分形树。我已经制作了这棵树,但我希望有两只或更多的乌龟可以同时绘制我的分形树。有没有办法做到这一点?我已经找到了解决方案,但没有一个是我真正想要的。这是我的代码:
import turtle
tree = turtle.Turtle()
tree.ht()
tree.penup()
tree.sety(-200)
tree.left(90)
import turtle
tree0 = turtle.Turtle()
tree0.ht()
tree0.penup()
tree0.sety(-200)
tree0.left(90)
startx = tree.xcor()
starty = tree.ycor()
startx = tree0.xcor()
starty = tree0.ycor()
def fractalright(angle, length, x, y):
tree.speed(0)
tree.setx(x)
tree.sety(y)
tree.pendown()
tree.forward(length)
tree.right(angle)
length = length - 20
x = tree.xcor()
y = tree.ycor()
if length < 0:
return
tree.penup()
fractalright(angle, length, x, y)
tree.penup()
tree.setx(x)
tree.sety(y)
tree.left(angle)
fractalright (-angle, length, x, y)
def fractalleft(angle, length, x, y):
tree0.speed(0)
tree0.setx(x)
tree0.sety(y)
tree0.pendown()
tree0.forward(length)
tree0.right(angle)
length = length - 20
x = tree0.xcor()
y = tree0.ycor()
if length < 0:
return
tree0.penup()
fractalleft(angle, length, x, y)
tree0.penup()
tree0.setx(x)
tree0.sety(y)
tree0.left(angle)
fractalleft (-angle, length, x, y)
我正在使用python 3,如果您知道解决方案,请告诉我。谢谢!
答案 0 :(得分:1)
提取出如何绘制树的细节,基本任务是与不同的参数并行执行一些绘制函数的两个实例,以指定一个“左树”和一个“右树”。这个基本结构可以实现如下:
from multiprocessing.dummy import Pool
import time
def draw_function(current_tree):
# Replace the following lines with what you need to do with turtles
print("Drawing tree {}".format(current_tree))
time.sleep(1)
# Replace this with list of tuples of arguments to draw_function specifying
# angle, length, x, y, left vs right, etc.
list_of_trees = ["left_tree", "right_tree"]
my_pool = Pool(2)
results = my_pool.map(draw_function, list_of_trees)
my_pool.close()
my_pool.join()
在您的情况下,我对fractalleft
和fractalright
之间的区别有点不清楚,因为它们看起来相同,但逻辑应构成draw_function
的基础。你应该为draw_function
的每次执行创建一个单独的乌龟,但请注意,你不需要重新导入乌龟。
答案 1 :(得分:0)
将模块threading
与turtle
一起使用的关键是不允许其他线程操纵海龟 - 他们改为排队他们的乌龟请求并让主线程处理它们:
import queue
from threading import Thread, active_count
from turtle import Turtle, Screen
def forward(turtle, distance):
graphics.put((turtle.forward, distance))
def right(turtle, angle):
graphics.put((turtle.right, angle))
def left(turtle, angle):
graphics.put((turtle.left, angle))
def fractalright(turtle, angle, length):
forward(turtle, length)
if length - 20 > 0:
right(turtle, angle)
fractalright(turtle, angle, length - 20)
left(turtle, angle)
fractalright(turtle, -angle, length - 20)
forward(turtle, -length)
def fractalleft(turtle, angle, length):
forward(turtle, length)
if length - 20 > 0:
left(turtle, angle)
fractalleft(turtle, angle, length - 20)
right(turtle, angle)
fractalleft(turtle, -angle, length - 20)
forward(turtle, -length)
def process_queue():
while not graphics.empty():
action, argument = graphics.get()
action(argument)
if active_count() > 1:
screen.ontimer(process_queue, 100)
START_X, START_Y = 0, -200
screen = Screen()
screen.mode('logo') # make starting direction 0 degrees towards top
tree1 = Turtle(visible=False)
tree1.color('green')
tree1.penup()
tree1.goto(START_X, START_Y)
tree1.pendown()
tree2 = Turtle(visible=False)
tree2.color('dark green')
tree2.penup()
tree2.goto(START_X, START_Y)
tree2.pendown()
graphics = queue.Queue(1) # size = number of hardware threads you have - 1
def fractal1():
fractalright(tree1, 30, 100)
def fractal2():
fractalleft(tree2, 30, 100)
thread1 = Thread(target=fractal1)
thread1.daemon = True # thread dies when main thread (only non-daemon thread) exits.
thread1.start()
thread2 = Thread(target=fractal2)
thread2.daemon = True # thread dies when main thread (only non-daemon thread) exits.
thread2.start()
process_queue()
screen.exitonclick()
我们正在使用队列模块进行线程安全通信。我重新编写了fractalright()
和fractalleft()
函数,以最大限度地减少所需的各种图形操作。
如果一切正常,您应该看到树的浅绿色和深绿色部分是同时独立绘制的。您的计算机至少需要有几个硬件线程。