Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- NextPoly[ps_] := Module[{i, j, p, q},
- i = RandomChoice[Range[Length[ps]]];
- j = RandomChoice[Complement[Range[Length[ps]], {i}]];
- If[j < i, {i, j} = {j, i}];
- p = With[{\[Theta] =
- RandomReal[]}, \[Theta] ps[[i]] + (1 - \[Theta]) ps[[
- Mod[i + 1, Length@ps, 1]]]];
- q = With[{\[Theta] =
- RandomReal[]}, \[Theta] ps[[j]] + (1 - \[Theta]) ps[[
- Mod[j + 1, Length@ps, 1]]]];
- {
- Join[{p}, Table[ps[[k]], {k, i + 1, j}], {q}],
- Join[{q},
- Table[ps[[Mod[k, Length@ps, 1]]], {k, j + 1,
- Length@ps + i}], {p}],
- p,
- q,
- ps
- }
- ];
- NextPoly2[nextpolyout_] :=
- Module[{poly1, poly2, p, q, poly1new, polyold},
- {poly1, poly2, p, q, polyold} = nextpolyout;
- poly1new = (Map[(p + q)/2 +
- ReflectionMatrix[Normalize@(p - q)].(# - (p + q)/2) &, poly1]);
- Join[poly1new, Reverse@poly2[[2 ;; -1]]]
- ];
- PolyArea[pts_] :=
- Abs[Apply[Plus,
- Flatten[pts Map[({1, -1} Reverse[#] &), RotateLeft[pts]]]]/2];
- Perp[n_] := {-n[[2]], n[[1]]}/Norm@n;
- InvertColor[col_] := RGBColor @@ (1 - List @@ col);
- n = 30;
- Generate[] := Module[{ps, m, p, found, candidate},
- ps = {};
- m = 6;
- p = {{-1, -0.61}, {1, -.61}, {1, 0.61}, {-1, 0.61}};
- Do[
- found = False;
- While[! found,
- candidate = NextPoly@p;
- found =
- PolyArea[candidate[[1]]] > 0.1 && PolyArea[candidate[[2]]] > 0.1;
- ];
- AppendTo[ps, candidate];
- p = NextPoly2@Last[ps];
- , {i, n}];
- ps
- ];
- frame[t_, ps_, col_] :=
- Module[{p1, poly1, poly2, p, q, poly1new, polynew, tt, polyold, xc,
- yc},
- p1 = ps[[1 + Floor[t]]];
- tt = t - Floor[t];
- {poly1, poly2, p, q, polyold} = p1;
- polynew = NextPoly2[p1];
- {xc, yc} = (1 - tt) Mean[polyold] + tt Mean[polynew];
- Graphics[{
- col,
- Polygon@poly2,
- Blend[{col, White}, 2 tt (1 - tt)],
- If[tt > 0.1 && tt < 0.9, {White, Line[{p, q}]}, {}],
- Polygon@(Map[(p + q)/
- 2 + ((1 - tt) IdentityMatrix[2] +
- tt ReflectionMatrix[Normalize@(p - q)]).(# - (p + q)/
- 2) &, poly1])
- }, ImageSize -> {300, 300}, PlotRange -> {
- {xc - 1.7, xc + 1.7},
- {yc - 1.7, yc + 1.7}
- }, AspectRatio -> 1]
- ];
- examples = Table[Generate[], {i, 9}];
- Manipulate[
- GraphicsGrid[
- Partition[
- Table[frame[Min[Max[0, t - i/9], n - 2], examples[[i]],
- ColorData["DarkRainbow"][i/9]], {i, Length@examples}], 3],
- ImageSize -> 300],
- {t, 0, n - 1}]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement