Advertisement
Matthen

Mutating tree

Jun 25th, 2011
334
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.34 KB | None | 0 0
  1. (* init with this:*)
  2. Needs["MultivariateStatistics`"];
  3. anim = {};
  4. mut = 0;
  5. n = 3;
  6. Clear[line]; Clear[tree];
  7. line[1, i_] :=
  8. line[1, i] = Accumulate[Join[{{0, 0}}, Table[{1, 0}, {10}]]];
  9. line[n_, i_] :=
  10. line[n, i] =
  11. SortBy[line[n - 1, i] +
  12. Join[{{0, 0}},
  13. RandomReal[NormalDistribution[0, 0.1], {10, 2}]], #[[1]] &];
  14. tree[i_] :=
  15. tree[i] =
  16. Table[Map[RotationMatrix[0.3 (k - (n + 1)/2)].# &,
  17. Join[{{0, 0}}, MovingAverage[line[i, k], 2]]], {k, 3}];
  18.  
  19. (* then run this a few times:*)
  20. Cl[n_, rot_, pos_, s_] :=
  21. ListLinePlot[
  22. Map[RotationMatrix[rot].(# s) + pos &,
  23. tree[mut max - mut n + 1], {2}], Joined -> True,
  24. InterpolationOrder -> 2, PlotRange -> All, AspectRatio -> 1,
  25. Axes -> None, PlotStyle -> Black];
  26. Crec[0, pos_, s_, rot_] = {Graphics[]};
  27. Crec[n_, pos_, s_, rot_] :=
  28. Module[{endpoints = Map[Last, tree[mut max - mut n + 1]]},
  29. Join[{Cl[n, rot, pos, s]},
  30. Crec[n - 1, pos + s RotationMatrix[rot].endpoints[[1]], s/3,
  31. rot - 0.3],
  32. Crec[n - 1, pos + s RotationMatrix[rot].endpoints[[2]], s/3, rot],
  33. Crec[n - 1, pos + s RotationMatrix[rot].endpoints[[3]], s/3,
  34. rot + 0.3]]
  35. ];
  36. max = 5;
  37. c = Show[Crec[max, {0, 0}, 1, 0], ImageSize -> {300}];
  38. anim = Append[anim, c];
  39. mut += 1;
  40.  
  41. (* then export: *)
  42.  
  43. Export["treemut.gif", Join[anim, Reverse@anim]];
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement