Don't like ads? PRO users don't see any ads ;-)

Hexagons inside a cube

By: Matthen on Nov 27th, 2011  |  syntax: None  |  size: 0.88 KB  |  hits: 144  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. p = Permutations[{0, 1, -1, 0, 1, -1, 0, 1, -1}, {3}];
  2. hexes = Flatten[ Table[{(-1)^i, (-1)^j, (-1)^k},
  3.      {i, 2}, {j, 2}, {k, 2}
  4.      ], 2][[;; 4]];
  5. hex[n_] :=
  6.   SortBy[ Select[p, (n.# == 0) && Norm[#] > 0 &], angle[n, #] &];
  7. Perp[n_] :=
  8.   Perp[n] =
  9.    First[{x, y, z} /.
  10.      FindInstance[{x, y, z}.n == 0 && (Norm[{x, y, z}]) > 0, {x, y,
  11.        z}, Reals]];
  12. angle[n_, v_] :=
  13.   Mod[VectorAngle[Perp[n], v] If[(Perp[n]\[Cross]v).n > 0, 1, -1],
  14.    2 Pi];
  15. anim[\[Theta]_] := Graphics3D[{
  16.     Opacity[0.5], Red, Cuboid[{-1, -1, -1}, {1, 1, 1}], Opacity[1],
  17.     White,
  18.     ,
  19.     Polygon[
  20.        hex[#]] & /@ (hexes[[;;
  21.          Round[4 TriangleWave[\[Theta]/(4 Pi)]]]])
  22.     }, PlotRange -> 1.1, Boxed -> False,
  23.    ViewPoint -> {5 Sin[\[Theta]/4], 5 Cos[\[Theta]/4], 2},
  24.    ViewAngle -> Pi/8];
  25. Manipulate[
  26.  anim[2 Pi (1 + ((\[Theta] - Pi)/(Pi))^3)/2], {\[Theta], 0, 2 Pi}]
  27.