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

Posted

技术标签:

【中文标题】在希尔伯特曲线中,如何将库龟从 matplotlib 更改为?【英文标题】:In the Hilbert Curve, How can I change the library turtle from to matplotlib? 【发布时间】:2021-08-03 00:52:39 【问题描述】:

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

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)

【问题讨论】:

【参考方案1】:

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

下面的代码实现了这些想法。为了使内容更具可读性,A 已重命名为 lengthn 已重命名为 levels

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()

【讨论】:

以上是关于在希尔伯特曲线中,如何将库龟从 matplotlib 更改为?的主要内容,如果未能解决你的问题,请参考以下文章

如何在不使用希尔伯特指数的情况下对希尔伯特曲线上的点进行排序?

稀疏几何的 3d 希尔伯特曲线

如何将按位函数从 matlab/C 转换为 R?特例:希尔伯特曲线算法

如何使用 Turtle [Python 3] 制作最有效的希尔伯特曲线?

如何按希尔伯特曲线的顺序将二维矩阵(n x n)展平为一维数组?

映射点与希尔伯特曲线