2

I am trying to align (colored) Cuboids within a cube, but the automatically coordinate generation "Row-2.)" is currently buggy. Can somebody help me please (the variable bound should be the start and define the cube-Length)!

1.) bound = 2;
2.) cb = IntegerDigits[Range[#], 10, 3] &[bound]
3.) Graphics3D[{Yellow, Cuboid[{0, 0, 0}], Blue, Table[Cuboid[cb[[i]]], {i, 1, 2^bound - 1, 1}]}, Boxed -> False]

By the way, how can I (automatically) fill each iteration with a different color ?

For example:

Cube-Iteration-1: Yellow (1 Cube)

Cube-Iteration-2: Blue (8 Cubes)

Cube-Iteration-3: Pink (27 Cubes)

...

enter image description here

Jorgos
  • 319
  • 2
  • 7

4 Answers4

5
m = {{{0}}};
m = Fold[
   ArrayPad[#, {{0, 1}, {0, 1}, {0, 1}}, #2] &,
   m,
   Range[9]
   ];

Graphics3D@Raster3D[m, ColorFunction -> (Blend["Rainbow", #/10.] &)]

enter image description here

Kuba
  • 136,707
  • 13
  • 279
  • 740
4
Graphics3D[Table[{Hue[RandomReal[]], Cuboid[{i, j, k}]}, {i, 3}, {j, 3}, {k, 3}]]

enter image description here

Or if you wish to specify colors:

mycolors = {
   {{Red, Green, Blue}, {Yellow, Orange, Black}, {Purple, Pink, Green}},
   {{White, Blue, Yellow}, {Red, Black, Red}, {Pink, Pink, Black}},
   {{Yellow, Green, Yellow}, {Purple, Orange, White}, {Green, Pink, Green}}
   };
 Graphics3D[
 Table[{mycolors[[i, j, k]], Cuboid[{i, j, k}]},
  {i, 3}, {j, 3}, {k, 3}]]

enter image description here

Include Boxed->False if you want to delete the box.

If you want to define "stages" of colors:

mynewcolors = Table[
  Which[Max[i, j, k] == 1, Blue, 
        Max[i, j, k] == 2, Green, 
        Max[i, j, k] == 3, Yellow], 
{i, 3}, {j, 3}, {k, 3}]

enter image description here

And it looks cooler if you make the cuboids semi-transparent:

mynewcolors = Table[
   Which[Max[i, j, k] == 1, Purple, 
    Max[i, j, k] == 2, Red, 
    Max[i, j, k] == 3, Green, 
    Max[i, j, k] == 4, Yellow], 
   {i, 4}, {j, 4}, {k, 4}];
   Graphics3D[
       Table[{Opacity[0.5], mynewcolors[[i, j, k]], Cuboid[{i, j, k}]},
       {i, 4}, {j, 4}, {k, 4}], Boxed -> False]

enter image description here

David G. Stork
  • 41,180
  • 3
  • 34
  • 96
4
cd = ColorData[3, "ColorList"]; 
cubes[iter_] := Graphics3D[ Table[{cd[[Mod[Max[i, j, k], Length@cd, 1]]], Cuboid[{i, j, k}]}, 
                                 {i, iter}, {j, iter}, {k, iter}], 
                            Boxed -> False, Axes -> False, Lighting -> {{"Ambient", White}}]

cubes[25]

Mathematica graphics

Alternatively:

cubes[i_] := Graphics3D[{ColorData[3, "ColorList"][[Max @@ #]], Cuboid@#} & /@  Tuples[Range@i, 3], 
                       Boxed -> False, Axes -> False, Lighting -> {{"Ambient", White}}]
Dr. belisarius
  • 115,881
  • 13
  • 203
  • 453
4

A concise refactor of David's code:

Graphics3D[{
  Opacity[0.5], 
  Array[
    {Switch[Max[##], 1, Purple, 2, Red, 3, Green, 4, Yellow], Cuboid[{##}]} &,
    {4, 4, 4}
  ]
}]

enter image description here

Mr.Wizard
  • 271,378
  • 34
  • 587
  • 1,371