这是我关于堆栈溢出的第一个问题。我正在写一个脚本来绘制一个林登迈尔系统。我已经知道如何使用turtle包来实现它,但我想使用matplotlib来实现。pyplot我有一个向量np.array[move,angle,move,angle,move,angle......]
中的所有转角和向前移动向量的长度是迭代次数的函数。例如L=1/3**N
,其中N是迭代次数,L是长度。向量的方向由下式给出
d=np.dot(np.array([[cos(angle[i],-sin(angle[i])],sin(angle[i]),cos(angle[i])),d)
其中第一个d应该是np.array[1,0]
第一个位置是x=(0,0)
,然后是x=x+L*d
,以此类推,但是我不能正确地迭代
我已经创建了几个脚本,以使我的LindenmayerSystem达到我希望的矢量形式。
import numpy as np
import math
from math import cos, sin
import matplotlib.pyplot as plt
System="Koch"
N=1
def LindIter(System, N):
if System == "Koch":
LindenmayerString = "S"
for i in range(N):
newseq = ""
for element in LindenmayerString:
newseq = newseq + {"S": "SLSRSLS"}.get(element, element)
LindenmayerString = newseq
elif System == "Sierpinski":
LindenmayerString = "A"
for i in range(N):
newseq = ""
for element in LindenmayerString:
newseq = newseq + {"A": "BRARB",
"B": "ALBLA"}.get(element, element)
LindenmayerString = newseq
# replace("A",{"A":"BRARB","B":"ALBLA"},N)
return LindenmayerString
def TurtleGraph(LindenmayerString):
TC = ""
if System == "Koch":
x = np.asarray(list(LindenmayerString), dtype=object)
x[x == "S"] = float(1/(3**N))
x[x == "L"] = float(1/3*math.pi)
x[x == "R"] = float(-2/3*math.pi)
TurtleCommands = x.astype(float)
elif System == "Sierpinski":
x = np.asarray(list(LindenmayerString), dtype=object)
x[x == "B"] = float(1/(2**N))
x[x == "A"] = float(1/(2**N))
x[x == "L"] = float(1/3*math.pi)
x[x == "R"] = float(-1/3*math.pi)
TurtleCommands = x.astype(float)
return TurtleCommands
我现在想要的操作是这样的1-迭代Koch系统,但我想在它上面扩展,所以我可以做更多的迭代:
vector=np.array([1/3,1/3*math.pi,1/3,-2/3*math.pi,1/3,1/3*math.pi,1/3])
N=1
Rads=TurtleCommands[1::2]
d0=np.array([1,0])
d1=np.dot(np.array([[cos(Rads[0]),-sin(Rads[0])],[sin(Rads[0]),cos(Rads[0])]]),d0)
d2=np.dot(np.array([[cos(Rads[1]),-sin(Rads[1])],[sin(Rads[1]),cos(Rads[1])]]),d1)
d3=np.dot(np.array([[cos(Rads[2]),-sin(Rads[2])],[sin(Rads[2]),cos(Rads[2])]]),d2)
x0=(0,0)
x1=x0+1/3**N*d0
x2=x1+1/3**N*d1
x3=x2+1/3**N*d2
x4=x3+1/3**N*d3
plotvals=np.append(x0,(x1,x2,x3,x4))
x=plotvals[0::2]
y=plotvals[1::2]
plt.plot(x,y)
plt.show()
[Plot of first iteration](https://i.stack.imgur.com/9w6Cx.png)
在我尝试创建的许多函数中,我得到了这个错误:
" d=np.dot(np.array([[cos(Rads[i]),-sin(Rads[i])],[sin(Rads[i]),cos(Rads[i])]]),d)
IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boolean arrays are valid indices"
我希望有人可以帮助,我的要求是合理的,在这个美好的论坛。
2条答案
按热度按时间sqxo8psd1#
我建议您不要创建
TurtleCommands
数组,而是可以立即创建一个图形点数组,例如称为Points
。我写了函数GraphPoints
来代替函数TurtleGraph
,它实现了这个想法。我从Wikipedia here得到了关于Koch和Sierpinski Lindenmayer系统的基本信息。jc3wubiy2#
我发现了如何做我想做的事情,虽然有一些帮助功能。
然后,我们得到的点:
而且可以压缩或切片它们:)