2
$\begingroup$

Here's some code that will produce a cube:

RegionPlot3D[
  x > 0 && x < 1 && y > 0 && y < 1 && z > 0 && z < 1,
  {x, -10, 10}, {y, -10, 10}, {z, -10, 10}, 
  PlotPoints -> 100]

What I'm hoping to figure out is a way to loop the code and creating several cubes, say 2 units apart. Essentially changing the equations to

x > 0 + m && x < 1 + m && y > 0 + m && y < 1 + m && z > 0 + m && z < 1 + m

where m goes from 0 to 8 by 2's --- {m, 0, 8, 2}.

I want to use equations instead of Cuboid. Also, I don't want to have to write separate equations for each cube. Once I know how to do this, I think I can figure out the rest of what I want to do.

$\endgroup$
1
$\begingroup$

You can make a table of the defining inequalities and combine them with Or. Like so.

RegionPlot3D[
  Or @@ 
    Table[
      x > 0 + m && x < 1 + m && y > 0 + m && y < 1 + m && z > 0 + m && z < 1 + m, 
      {m, 0, 8, 2}],
  {x, 0, 11}, {y, 0, 11}, {z, 0, 11}, 
  PlotPoints -> 50]

plot

$\endgroup$
1
$\begingroup$

You can also use Translate on the graphics primitives produced by RegionPlot3D:

rp = RegionPlot3D[x > 0 && x < 1 && y > 0 && y < 1 && z > 0 && z < 1, 
  {x, 0, 11}, {y,  0, 11}, {z, 0, 11}, PlotPoints -> 150, Mesh -> 3];

translations = Table[{m, m, m}, {m, 0, 8, 2}];
MapAt[Translate[#, translations] &, rp, {1}]

enter image description here

Alternatively,

Graphics3D[Translate[rp[[1]], translations], Axes -> True, PlotRange -> PlotRange[rp]]

same picture

You can also use a combination of GeometricTransformation and TranslationTransform:

Graphics3D[GeometricTransformation[rp[[1]], TranslationTransform /@ translations]]

same picture

$\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.