使用Mathematica绘制Sierpinski地毯

在Mathematica中内置的绘制Sierpinski地毯的函数:

SierpinskiCurve[n] 
gives the line segments representing the n-step Sierpiński curve.

注意,直接运行这个函数,返回的是Line对象,例如:

运行如下代码:

GraphicsGrid[ { {Graphics[SierpinskiCurve[1]], Graphics[SierpinskiCurve[2]]}, {Graphics[SierpinskiCurve[3]], Graphics[SierpinskiCurve[4]]} } ]

我们可以得到前面几个地毯的对比图形:

可以通过DataRange,指定参数范围,使得相应的几何图形,能够放在合适的坐标系内:

Graphics[ SierpinskiCurve[3, DataRange -> {{0, 1}, {0, 1}}], PlotRange -> 2, Axes -> True ]

在这个给定的坐标系内,可使用BSplineFunction,将几何图形参数化并绘制出来:

BSplineFunction[ SierpinskiCurve[3, DataRange -> {{-1, 1}, {-1, 1}}][[1]], SplineDegree -> 1]; ParametricPlot[%[t], {t, 0, 1}, PlotPoints -> 400]

也可以采用如下方式来实现:

Graphics[{Thickness[Large], SierpinskiCurve[3] /. Line -> BSplineCurve}]

注意:此时的BSplineFunction的阶数为2(看起来比上面的要更加光滑)。

利用这个参数化,可以直观看到曲线的填充过程:

tempFunc00 = BSplineFunction[ SierpinskiCurve[4, DataRange -> {{-1, 1}, {-1, 1}}][[1]], SplineDegree -> 1]; Animate[ParametricPlot[tempFunc00[t], {t, 0, n}, PlotPoints -> 400, PlotRange -> {{-1, 1}, {-1, 1}}], {n, 0.01, 1, 0.01}]

这样的动态效果,是有连续性的视觉效果。但是如果绘制图形的阶数比较大,也可以使用“整数”的方式,来达到类似的视觉效果。

With[{curve = SierpinskiCurve[6]}, Manipulate[ Graphics[{curve, Red, Thick, Line[Take[First[curve], i]]}, ImageSize -> Medium], {i, 1, 16385, 1}]]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值