首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在希尔伯特曲线中,我如何将库乌龟从更改为matplotlib?

在希尔伯特曲线中,我如何将库乌龟从更改为matplotlib?
EN

Stack Overflow用户
提问于 2021-05-13 00:16:16
回答 1查看 58关注 0票数 2

我真的不知道如何在保留递归的情况下解决问题,我正在研究海龟库,但在签名中他们要求使用matplotlib,所以我不太确定如何使用它

代码语言:javascript
复制
from turtle import Turtle


def hilbert_curve(turtle, A, parity, n):

    if n == 1:
        hilbert_curve_draw(turtle, A, parity)
        
    else:
        turtle.right(parity * 90)
        hilbert_curve(turtle, A, - parity, n - 1)
        
        turtle.forward(A)
        turtle.left(parity * 90)
        hilbert_curve(turtle, A, parity, n - 1)
        
        turtle.forward(A)
        hilbert_curve(turtle, A, parity, n - 1)
        turtle.left(parity * 90)
        
        turtle.forward(A)
        hilbert_curve(turtle, A, - parity, n - 1)
        turtle.right(parity * 90)


def hilbert_curve_draw(turtle, A, parity):
    turtle.right(parity * 90)
    turtle.forward(A)
    turtle.left(parity * 90)
    turtle.forward(A)
    turtle.left(parity * 90)
    turtle.forward(A)
    turtle.right(parity * 90)
    
yertle = Turtle()

yertle.hideturtle() 
yertle.penup() 
yertle.goto(-200, 200)   
yertle.pendown() 
yertle.speed('fastest')
 
hilbert_curve(yertle, 60, 1, 3)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-05-13 15:15:47

乌龟的位置和方向保持着一种内部状态。这些可以用局部变量来表示。方向可以是表示长度为1的向量的元组(xdir, ydir)。向左旋转将使向量与旋转矩阵相乘。当仅使用坐标为-101的向量时,可以简化旋转。

下面的代码实现了这些想法。为了使代码更具可读性,将A重命名为length,将n重命名为levels

代码语言:javascript
复制
from matplotlib import pyplot as plt

def turn_left(orient, parity):
    return (orient[1] * parity, -orient[0] * parity)

def turn_right(orient, parity):
    return turn_left(orient, -parity)

def draw_line(x0, y0, orient, length):
    x1 = x0 + orient[0] * length
    y1 = y0 + orient[1] * length
    plt.plot([x0, x1], [y0, y1], color='navy')
    return x1, y1

def hilbert_curve_draw(x, y, length, orient, parity):
    orient = turn_right(orient, parity)
    x, y = draw_line(x, y, orient, length)
    orient = turn_left(orient, parity)
    x, y = draw_line(x, y, orient, length)
    orient = turn_left(orient, parity)
    x, y = draw_line(x, y, orient, length)
    orient = turn_right(orient, parity)
    return x, y, orient

def hilbert_curve(x, y, length, orient, parity, levels):
    if levels == 1:
        x, y, orient = hilbert_curve_draw(x, y, length, orient, parity)
    else:
        orient = turn_right(orient, parity)
        x, y, orient = hilbert_curve(x, y, length, orient, - parity, levels - 1)

        x, y = draw_line(x, y, orient, length)
        orient = turn_left(orient, parity)
        x, y, orient = hilbert_curve(x, y, length, orient, parity, levels - 1)

        x, y = draw_line(x, y, orient, length)
        x, y, orient = hilbert_curve(x, y, length, orient, parity, levels - 1)
        orient = turn_left(orient, parity)

        x, y = draw_line(x, y, orient, length)
        x, y, orient = hilbert_curve(x, y, length, orient, - parity, levels - 1)
        orient = turn_right(orient, parity)
    return x, y, orient

hilbert_curve(x=-200, y=200, length=5, orient=(1, 0), parity=1, levels=5)
plt.axis('equal') # same lengths in x and y direction
plt.show()

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67507363

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档