谢尔宾斯基三角形(Sierpinski triangle)

stackverflow上看到一个php版的,

$x = 200;
$y = 200;
$gd = imagecreatetruecolor($x, $y);
$corners[0] = array('x' => 100, 'y' =>  10);
$corners[1] = array('x' =>   0, 'y' => 190);
$corners[2] = array('x' => 200, 'y' => 190);
$red = imagecolorallocate($gd, 255, 0, 0); 
for ($i = 0; $i < 100000; $i++) {
  imagesetpixel($gd, round($x),round($y), $red);
  $a = rand(0, 2);
  $x = ($x + $corners[$a]['x']) / 2;
  $y = ($y + $corners[$a]['y']) / 2;
}
header('Content-Type: image/png');
imagepng($gd);

用Mathematica画画

version1

a[0] = {x -> 100, y -> 10};
a[1] = {x -> 0, y -> 190};
a[2] = {x -> 200, y -> 190};
m = 200;
n = 200;
pts = Table[r = RandomInteger[{0, 2}];
   m = (m + x /. a[r])/2;
   n = (n + y /. a[r])/2; {m, n}, {1000}];
Graphics[Point@-pts]

version 2

a = N@{{100, 10}, {0, 190}, {200, 190}};
{m, n} = {200, 200};
pts = Table[{r1, r2} = RandomChoice[a];
   {m, n} = {(m + r1)/2, (n + r2)/2}, {1000}];
Graphics[Point@-pts]

version 3

ListPlot@NestList[(# + RandomChoice[{{0, 0}, {1, 0}, {.5, .8}}])/ 2 &, {0., 0.}, 10^3]

很容易推广到空间中

With[{v = {{0, 0, 0.6}, {-0.3, -0.5, -0.2},
    {-0.3, 0.5, -0.2}, {0.6, 0, -0.2}}},
 ListPointPlot3D[
  NestList[(# + RandomChoice[v])/2 &, {0., 0., 0}, 2 10^4], 
  BoxRatios -> 1, ColorFunction -> "Pastel"]]

 

转载于:https://www.cnblogs.com/c-hy/archive/2013/03/23/2977843.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值