微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在python海龟图形中找到顶点?

如何解决如何在python海龟图形中找到顶点?

我在 python 中使用乌龟模块生成分形树。要计算树的分形维数,我需要知道树顶点的 y 坐标。我使用 pyautogui.position() 通过将鼠标指向树的顶部来手动查找树的高度。这需要很长时间,因此我的问题来了:

是否有内置函数可以在海龟中找到图形的最大高度?如果没有,是否有其他方法可以找到它?我附上了下面制作的图片的例子。提前致谢。

Example of generated fractal tree

enter image description here

解决方法

设置一个变量来跟踪最大 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  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。