13
$\begingroup$

enter image description here

I wrote the following to draw a pyramid, this approach uses functions TranslationTransform and Tuples, is there a way to do the same thing more concise? Maybe Table, Nest, ArrayPad is useful.

cubes = {
   {c1 = Cuboid[]},
   TranslationTransform[{##, -1}][c1] & @@@ Tuples[{-1, 1}/2, 2],
   TranslationTransform[{##, -2}][c1] & @@@ Tuples[{-1, 0, 1}, 2],
   TranslationTransform[{##, -3}][c1] & @@@ Tuples[{-3, -1, 1, 3}/2, 2],
   TranslationTransform[{##, -4}][c1] & @@@ Tuples[{-2, -1, 0, 1, 2}, 2]
   };
Graphics3D[cubes]

Thank you all.

$\endgroup$
  • $\begingroup$ Please don't include the answer in your question -- post it as an answer, instead. $\endgroup$ – David Richerby May 30 at 18:21
  • $\begingroup$ @DavidRicherby All right. $\endgroup$ – mathe May 31 at 2:22
10
$\begingroup$
n = 5;
Graphics3D[Table[Cuboid[{#[[1]], #[[2]], -i}] & /@ Tuples[Range[-i/2, i/2], 2], 
 {i, 0, n - 1}]]

enter image description here

A more concise version (thanks: Vitaliy Kaurov):

Graphics3D[Table[Cuboid[{#, #2, -i}] & @@@ Tuples[Range[-i/2, i/2], 2], {i, 0, n - 1}]]
$\endgroup$
  • 2
    $\begingroup$ NIce +1 ! For code-golf i'd pack this as Graphics3D[Table[Cuboid[{#1,#2,-i}]&@@@Tuples[Range[-i,i]/2,2], {i,0,4}]] ;-) I deleted my answer, it was essentially the same. $\endgroup$ – Vitaliy Kaurov May 30 at 10:21
  • $\begingroup$ Thank you @Vitaliy. Updated with you suggestion. $\endgroup$ – kglr May 30 at 11:02
3
$\begingroup$

Inspired by @kglr, now I got another simpler way

Graphics3D[Table[Cuboid[{x,y,-z}],{z,0,4},{y,-z/2,z/2},{x,-z/2,z/2}]]

or

Graphics3D[Table[{Cuboid[{x-z/2,y-z/2,-z}]},{z,5},{y,z},{x,z}]]
$\endgroup$
2
$\begingroup$

Not concise

n = 7;

Standardize[GraphEmbedding@GridGraph@#, Mean, 1&] & /@ Array[{#,#}&, n] // 
Cuboid /@ Join@@MapIndexed[Append[#1, - First@#2 +.5]&, #, {2}] & // Graphics3D

enter image description here

but fun to play with:

 n = 20;

 Standardize[ #(CycleGraph@#~GraphEmbedding~"CircularEmbedding"), Mean, 1&] & /@ Array[#&, n] // 
 Ball /@ Join@@MapIndexed[Append[#1, - 1.5 First@#2 +.5]&, #, {2}] & // Graphics3D

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.