# How can I align Cuboids within a cube

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)

... 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 Alternatively:

cubes[i_] := Graphics3D[{ColorData[3, "ColorList"][[Max @@ #]], Cuboid@#} & /@  Tuples[Range@i, 3],
Boxed -> False, Axes -> False, Lighting -> {{"Ambient", White}}]

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

Graphics3D@Raster3D[m, ColorFunction -> (Blend["Rainbow", #/10.] &)] • Hey! No Cuboids! :) – Dr. belisarius Feb 12 '15 at 18:36
• @belisarius I can see at least 3 :) – Kuba Feb 12 '15 at 18:42
Graphics3D[Table[{Hue[RandomReal[]], Cuboid[{i, j, k}]}, {i, 3}, {j, 3}, {k, 3}]] 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}]] 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}] 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] 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}
]
}] • Yes. Much cleaner. – David G. Stork Feb 12 '15 at 18:54