Advertisement
Matthen

Flipping Rectangles

Sep 12th, 2014
837
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.45 KB | None | 0 0
  1. NextPoly[ps_] := Module[{i, j, p, q},
  2. i = RandomChoice[Range[Length[ps]]];
  3. j = RandomChoice[Complement[Range[Length[ps]], {i}]];
  4. If[j < i, {i, j} = {j, i}];
  5. p = With[{\[Theta] =
  6. RandomReal[]}, \[Theta] ps[[i]] + (1 - \[Theta]) ps[[
  7. Mod[i + 1, Length@ps, 1]]]];
  8. q = With[{\[Theta] =
  9. RandomReal[]}, \[Theta] ps[[j]] + (1 - \[Theta]) ps[[
  10. Mod[j + 1, Length@ps, 1]]]];
  11. {
  12. Join[{p}, Table[ps[[k]], {k, i + 1, j}], {q}],
  13. Join[{q},
  14. Table[ps[[Mod[k, Length@ps, 1]]], {k, j + 1,
  15. Length@ps + i}], {p}],
  16. p,
  17. q,
  18. ps
  19. }
  20. ];
  21. NextPoly2[nextpolyout_] :=
  22. Module[{poly1, poly2, p, q, poly1new, polyold},
  23. {poly1, poly2, p, q, polyold} = nextpolyout;
  24. poly1new = (Map[(p + q)/2 +
  25. ReflectionMatrix[Normalize@(p - q)].(# - (p + q)/2) &, poly1]);
  26. Join[poly1new, Reverse@poly2[[2 ;; -1]]]
  27. ];
  28. PolyArea[pts_] :=
  29. Abs[Apply[Plus,
  30. Flatten[pts Map[({1, -1} Reverse[#] &), RotateLeft[pts]]]]/2];
  31. Perp[n_] := {-n[[2]], n[[1]]}/Norm@n;
  32. InvertColor[col_] := RGBColor @@ (1 - List @@ col);
  33. n = 30;
  34. Generate[] := Module[{ps, m, p, found, candidate},
  35. ps = {};
  36. m = 6;
  37. p = {{-1, -0.61}, {1, -.61}, {1, 0.61}, {-1, 0.61}};
  38. Do[
  39. found = False;
  40. While[! found,
  41. candidate = NextPoly@p;
  42. found =
  43. PolyArea[candidate[[1]]] > 0.1 && PolyArea[candidate[[2]]] > 0.1;
  44. ];
  45. AppendTo[ps, candidate];
  46. p = NextPoly2@Last[ps];
  47. , {i, n}];
  48. ps
  49. ];
  50. frame[t_, ps_, col_] :=
  51. Module[{p1, poly1, poly2, p, q, poly1new, polynew, tt, polyold, xc,
  52. yc},
  53. p1 = ps[[1 + Floor[t]]];
  54. tt = t - Floor[t];
  55. {poly1, poly2, p, q, polyold} = p1;
  56. polynew = NextPoly2[p1];
  57. {xc, yc} = (1 - tt) Mean[polyold] + tt Mean[polynew];
  58. Graphics[{
  59. col,
  60. Polygon@poly2,
  61. Blend[{col, White}, 2 tt (1 - tt)],
  62. If[tt > 0.1 && tt < 0.9, {White, Line[{p, q}]}, {}],
  63. Polygon@(Map[(p + q)/
  64. 2 + ((1 - tt) IdentityMatrix[2] +
  65. tt ReflectionMatrix[Normalize@(p - q)]).(# - (p + q)/
  66. 2) &, poly1])
  67. }, ImageSize -> {300, 300}, PlotRange -> {
  68. {xc - 1.7, xc + 1.7},
  69. {yc - 1.7, yc + 1.7}
  70. }, AspectRatio -> 1]
  71. ];
  72. examples = Table[Generate[], {i, 9}];
  73. Manipulate[
  74. GraphicsGrid[
  75. Partition[
  76. Table[frame[Min[Max[0, t - i/9], n - 2], examples[[i]],
  77. ColorData["DarkRainbow"][i/9]], {i, Length@examples}], 3],
  78. ImageSize -> 300],
  79. {t, 0, n - 1}]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement