我正在使用python中的turtle模块生成分形树。为了计算树的分形维数,我需要知道树顶点的y坐标。我使用pyautogui.position()手动查找树的高度,方法是将鼠标指向树的顶部。这需要很长时间,所以我的问题来了:
有没有内置的函数来找出海龟的最大绘图高度?如果没有,有没有其他方法可以找到它?我附上了下面制作的图片的例子。提前谢谢你。
Example of generated fractal tree
发布于 2021-05-18 20:24:38
设置一个变量来跟踪最大Y位置,在绘图函数期间更新它,然后在这些函数完成分形后将其置零。
#! /usr/bin/python3
from turtle import Turtle, Screen
turtle, screen = Turtle(), Screen()
##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
screen.setup( width = 600, height = 600 )
screen.title('Maximum Recursion')
turtle.speed( 0 )
maxY = 0 ## empty forward-declaration
def recursive_draw( step, length ):
global maxY
turtle.forward( length )
turtle.right( 7 )
if turtle.ycor() > maxY: maxY = turtle.ycor() ## update when needed
if step > 0: recursive_draw( step -1, length *0.8 )
for i in range( 50, 130 ):
turtle.penup()
turtle.setheading( i )
turtle.setpos( 0, -300 )
turtle.pendown()
recursive_draw( 20, i )
turtle.hideturtle()
print( maxY )
screen.exitonclick()
## eof ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
https://stackoverflow.com/questions/67545288
复制相似问题