谢尔宾斯基三角形(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]

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

很容易推广到空间中

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"]]

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

 

你可能感兴趣的:(RIA)