# ぱらぱらめくる『The algorithmic beauty of plants』３　ブッシュな木

• 昨日の続き
• ３次元でも
• 伸びるにしたがって、枝が短くなるように
• 葉を入れたり
• ３次元の座標はとれたけれど、描図技術が今一つなので。。。

[
[

```#
# U,V :Ru
# M,N :Rl
# P,Q :Rh
# W   :pi

ResultTurtle3<-function(x,d=1,D=pi/2,initX=NULL,initA=NULL){
Ru<-matrix(c(cos(D),sin(D),0,-sin(D),cos(D),0,0,0,1),3,3,byrow=TRUE)
Rl<-matrix(c(cos(D),0,-sin(D),0,1,0,sin(D),0,cos(D)),3,3,byrow=TRUE)
Rh<-matrix(c(1,0,0,0,cos(D),-sin(D),0,sin(D),cos(D)),3,3,byrow=TRUE)
Ru2<-matrix(c(cos(-D),sin(-D),0,-sin(-D),cos(-D),0,0,0,1),3,3,byrow=TRUE)
Rl2<-matrix(c(cos(-D),0,-sin(-D),0,1,0,sin(-D),0,cos(-D)),3,3,byrow=TRUE)
Rh2<-matrix(c(1,0,0,0,cos(-D),-sin(-D),0,sin(-D),cos(-D)),3,3,byrow=TRUE)
Rw<-matrix(c(-1,0,0,0,-1,0,0,0,1),3,3,byrow=TRUE)
Niter<-length(x)
X<-matrix(0,Niter+1,3)
T<-diag(rep(1,3))
Ts<-NULL
Ts[[1]]<-T
Y<-X
L<-rep(0,Niter)
K<-rep(0,Niter)
K[1]<-d
if(is.null(initX)){
initX<-c(0,0)
}
if(is.null(initA)){
initA<-0
}
X[1,]<-c(initX,initA)
Stocker<-c()
for(i in 1:length(x)){
X[i+1,]<-X[i,]
Ts[[i+1]]<-Ts[[i]]
K[i+1]<-K[i]
if(x[i]=="F"){
X[i+1,1]<-X[i+1,1]+K[i]*cos(X[i,3])
X[i+1,2]<-X[i+1,2]+K[i]*sin(X[i,3])
L[i]<-1
}else if(x[i]=="f"){
X[i+1,1]<-X[i+1,1]+K[i]*cos(X[i,3])
X[i+1,2]<-X[i+1,2]+K[i]*sin(X[i,3])
L[i]<-0
}else if(x[i]=="A"){
L[i]<-2
}else if(x[i]=="S"){
L[i]<-3
}else if(x[i]=="L"){
L[i]<-4
}else if(x[i]=="U"){
Ts[[i+1]]<-Ru%*%Ts[[i+1]]
}else if(x[i]=="V"){
Ts[[i+1]]<-Ru2%*%Ts[[i+1]]
}else if(x[i]=="M"){
Ts[[i+1]]<-Rl%*%Ts[[i+1]]

}else if(x[i]=="N"){
Ts[[i+1]]<-Rl2%*%Ts[[i+1]]

}else if(x[i]=="P"){
Ts[[i+1]]<-Rh%*%Ts[[i+1]]

}else if(x[i]=="Q"){
Ts[[i+1]]<-Rh2%*%Ts[[i+1]]

}else if(x[i]=="W"){
Ts[[i+1]]<-Rw%*%Ts[[i+1]]

}else if(x[i]=="!"){
K[i+1]<-K[i]*0.9
}else if(x[i]=="'"){
X[i+1,3]<-X[i+1,3]+D
L[i]<-1

}else if(x[i]=="{"){
X[i+1,3]<-X[i+1,3]+D
L[i]<-1

}else if(x[i]=="}"){
X[i+1,3]<-X[i+1,3]+D
L[i]<-1

}else if(x[i]=="["){
Stocker<-c(Stocker,i+1)
L[i]<-1

}else if(x[i]=="]"){
X[i+1,]<-X[Stocker[length(Stocker)],]
L[i]<-1
K[i+1]<-K[Stocker[length(Stocker)]]
Stocker<-Stocker[1:(length(Stocker)-1)]
}
Y[i,]<-X[i+1,]
}
list(X=X,Y=Y,L=L,x=x)
}
DrawTurtle3<-function(ttl,col=FALSE){
library(rgl)
if(col){
plot3d(ttl\$X[,1],ttl\$X[,2],ttl\$X[,3],col=ttl\$L,cex=ttl\$L,cex=0.1)
}else{
plot3d(ttl\$X[,1],ttl\$X[,2],ttl\$X[,3],cex=0.1)
}

#xlim<-ylim<-range(ttl\$X[,1:2])
#plot(ttl\$X[,1],ttl\$X[,2],cex=0.1,xlim=xlim,ylim=ylim)
XY<-cbind(ttl\$X,ttl\$Y)
XYc<-c(t(XY))
XY2<-matrix(XYc,ncol=3,byrow=TRUE)
segments3d(XY2,col=c(1,ttl\$L))
#for(i in 1:length(ttl\$L)){
#if(ttl\$L[i]==1){
#c<-1
#if(col)c<-ttl\$L[i]
#segments3d(ttl\$X[i,],ttl\$Y[i,],col=c)
#}
#}

}

elem<-c("A","S","[","]","L","!","'","U","V","M","N","P","Q","W","{","}")

initX<-c("A")
replacer<-hash(elem,elem)
replacer[["A"]]<-"[MFL!A]QQQQQ'[&FL!A]QQQQQQQ'[MFL!A]"

replacer[["F"]]<-"SQQQQQF"
replacer[["S"]]<-"FL"
replacer[["L"]]<-"['''NN{VfUfUfVWVfUfUf}]"
Nstep<-5

X<-MakeSequence2(initX,Nstep,replacer)
X[[Nstep+1]]

D<-22.5/180*pi
ttl<-ResultTurtle3(X[[Nstep+1]],D=D)
DrawTurtle3(ttl,col=TRUE)
DrawTurtle2(ttl)
```