# 用Wolfram语言构建三维勾股树

```Clear["`*"];
next[{a_,b_,c_,d_},t_:Pi/6]:=
{
{a,b,c,d}//TranslationTransform[c-b]//RotationTransform[t-Pi/2,c]//ScalingTransform[{1,1}Sin[t],c],
{a,b,c,d}//TranslationTransform[c-b]//RotationTransform[t,d]//ScalingTransform[{1,1}Cos[t],d]
};
pts=NestList[Join@@next/@#&,N@{{{0,0},{1,0},{1,1},{0,1}}},5];
Graphics[Polygon/@pts]```

```Clear["`*"];
next[{a1_,b1_,c1_,d1_,a2_,b2_,c2_,d2_},t_:Pi/6]:=
{
{a1,b1,c1,d1,a2,b2,c2,d2}//TranslationTransform[a2-a1]//RotationTransform[t,{0,-1,0},(a2+d2)/2]//ScalingTransform[{1,1,1}Cos[t],(a2+d2)/2],
{a1,b1,c1,d1,a2,b2,c2,d2}//TranslationTransform[a2-a1]//RotationTransform[Pi/2-t,{0,1,0},(b2+c2)/2]//ScalingTransform[{1,1,1}Sin[t],(b2+c2)/2]
};
n = 15;
pts=NestList[Join@@next/@#&,N@{{{0,0,0},{1,0,0},{1,1,0},{0,1,0},{0,0,1},{1,0,1},{1,1,1},{0,1,1}}},n];
Graphics3D[Hexahedron/@pts]```

```Clear["`*"];
next=
{a1,b1,c1,d1,a2,b2,c2,d2}/.v_:>(
{
v//TranslationTransform[a2-a1]//RotationTransform[-t,{0,1,0},(a2+d2)/2]//ScalingTransform[{1,1,1}Cos[t],(a2+d2)/2],
v//TranslationTransform[a2-a1]//RotationTransform[Pi/2-t,{0,1,0},(b2+c2)/2]//ScalingTransform[{1,1,1}Sin[t],(b2+c2)/2]
}/.
expr_:>Compile[{{A,_Real,2},{t,_Real}},
expr,RuntimeAttributes->{Listable}
]
);
n=15;
pts=Join@@MapIndexed[Flatten[#,#2[[1]]-1]&,NestList[next[#,Pi/4.]&,N@{{{0,0,0},{1,0,0},{1,1,0},{0,1,0},{0,0,1},{1,0,1},{1,1,1},{0,1,1}}},n]];//AbsoluteTiming

Graphics3D[{EdgeForm[],Hexahedron@pts},ImageSize->Full]```

Wolfram 语言是不是太酷了！欢迎点击左上方蓝字“WOLFRAM”关注我们，查看更多历史文章.

4 用Wolfram语言构建第八代“神奇宝贝（Pokémon）”

295 篇文章57 人订阅

0 条评论