我已经制作了 3 个函数来绘制希尔伯特曲线。这更像是一个数学问题,但我会包括编码以防万一。我使用turtle python模块来绘制。
第一个,这个让我推导出一个列表。
def derive(c1,c2,l,l1,l2):
"""
derive list l by list l1 for every c1 element and by list2 for every
c2 element. designed for hilbert fractal and dragon fractal
:param c1: element by which l2 will derive l1
:type c1: str
:param c2: element by which l2 will derive l1
:type c2: str
:param l: list to be derived
:type l: list
:param l1: list to derive with
:type l1: list
:param l2: list to derive with
:type l2: list
:return: list
:CU: l1 and l2 must only contain these elements : 'R','L','F','+','-'
:Example:
>>>derive('-','A',['F','-','F'],['+','+','F'])
['F', '+', '+', 'F', 'F']
"""
assert type(l) in {list} and type(l1) in {list} and type(l2) in {list},'parameter l1 or l2 must be a list'
assert type(c1) in {str} and type(c2) in {str},'parameter c1 and c2 must be a string'
lret = []
for e in l:
if e != c1 and e!= c2:
# assert type(e) in {str},'parameter l must only contain str'
# assert e == 'R' or e == 'L' or e == 'F' or e == '+' or e == '-','parameter l1 elements must be \'R\' or \'L\' or \'F\' or \'-\' or \'+\''
lret.append(e)
elif e == c1:
for e1 in l1:
# assert type(e1) in {str},'parameter l1 must only contain str'
# assert e1 == 'R' or e1 == 'L' or e1 == 'F' or e1 == '+' or e1 == '-','parameter l1 elements must be \'R\' or \'L\' or \'F\' or \'-\' or \'+\''
lret.append(e1)
elif e == c2:
for e2 in l2:
# assert type(e2) in {str},'parameter l2 must only contain str'
# assert e2 == 'R' or e2 == 'L' or e2 == 'F' or e2 == '+' or e2 == '-','parameter l1 elements must be \'R\' or \'L\' or \'F\' or \'-\' or \'+\''
lret.append(e2)
return lret
第二个,这个派生第n次
def derive_n(n,c1,c2,l,l1,l2):
"""
derive list l1 by list l2 for every c element n-th times
:param c1: element by which l1 will derive l
:type c1: str
:param c2: element by which l2 will derive l
:type c2: str
:param l: list to be derived
:type l: list
:param l1: list to derived with
:type l1: list
:param l2: list to derive with
:type l2: list
:param n: number of time l1 will be derived
:type n: int
:return: list
:CU: n >= 0
:Example:
>>>derive_n(0,'F','L',['F','-','F'],['F','+','F'],['L','R'])
['F', '-', 'F']
>>>derive_n(1,'F','L',['F','-','F'],['F','+','F'],['L','R'])
['F', '+', 'F', '-', 'F', '+', 'F']
>>>derive_n(2,'F','L',['F','-','F'],['F','+','F'],['L','R'])
['F', '+', 'F', '+', 'F', '+', 'F', '-', 'F', '+', 'F', '+', 'F', '+', 'F']
"""
if n == 0:
return l
else:
return derive(c1,c2,derive_n(n-1,c1,c2,l,l1,l2),l1,l2)
第三个函数绘制曲线:
def draw(il,l,a):
"""
draw a fractal by following parameter il
:param il: instruction list
:type il: list
:param l: length for forward() function
:type l: float or int
:param a: angle for left and right function in degree
:type a: float or int
:CU: l > 0
"""
assert type(a) in {int,float},'parameter a must be an int or float'
assert type(l) in {int,float},'parameter l must be an int or float'
assert type(il) in {list},'parameter il must be a list'
assert l > 0,'parameter l must be strictly superior to 0'
board_reset()
pendown()
for e in il:
if e == 'F':
forward(l)
elif e == '+':
left(a)
elif e == '−':
right(a)
penup()
boardrese() 是一个重新初始化绘图板的函数。
这是我必须为课堂做的一个项目。我几乎完成了,但根据我的教授的说法,无论您导出多少次列表,绘图必须始终填充一个大小不变的正方形。
基本上,我需要对绘图函数的长度参数做一些数学运算。我只是不知道是什么。我试过 l/n,l/(“F”出现在最终列表中的次数),l/(最终列表的长度)...
谢谢