2024-10-04 05:31:30 发布
网友
我正在使用python中的turtle模块生成分形树。为了计算树的分维,我需要知道树的顶点的y坐标。我使用pyautogui.position()手动查找树的高度,方法是将鼠标指向树的顶部。这需要很长时间,所以我的问题来了:
有并没有内置函数来查找海龟图的最大高度?如果没有,还有其他方法找到它吗?我已经附上下面的图片制作的例子。先谢谢你
Example of generated fractal tree
设置一个变量以跟踪最大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 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
设置一个变量以跟踪最大Y位置,在绘图函数期间更新它,然后在这些函数完成分形后将其归零
相关问题 更多 >
编程相关推荐