17
$\begingroup$

I'm trying to create a region of "thick" grid-lines, like so:

GRID = ImplicitRegion[((0 <= x <= 1 || 2 <= x <= 3 || 4 <= x <= 5 || 6 <= x <= 7 || 
   8 <= x <= 9) || (0 <= y <= 1 || 2 <= y <= 3 || 4 <= y <= 5 || 
   6 <= y <= 7 || 8 <= y <= 9)), {{x, 0, 9}, {y, 0, 9}}];

which with RegionPlot[GRID] produces

plot of GRID

However, I would like to be able to make my grid an arbitrary size, and arbitrary thickness. So far I have tried using

GRID2 = ImplicitRegion[Mod[x, 5] <= 1 || Mod[y, 5] <= 1, {{x, -10, 10}, {y, -10, 10}}]

but when I try to plot this, Mathematica tells me it's an invalid region to plot. I've also tried Floor-ing the x and y prior to Mod-ing, but it makes no difference. Are there any suggestions on a better way of doing this?

EDIT: It's probably worth pointing out that I plan on using this region as the domain specification of a ParametricPlot3D.

$\endgroup$

3 Answers 3

15
$\begingroup$
r = N @ ImplicitRegion[
   Sin[x Pi] > 0 || Sin[y Pi] > 0, 
   {{x, 0, 9}, {y, 0, 9}}
]

RegionPlot @ r

enter image description here

r3 = N @ ImplicitRegion[
   Sin[x Pi] > 0 || Sin[y Pi] > 0 || Sin[z Pi] > 0, 
   {{x, 0, 9}, {y, 0, 9}, {z, 0, 9}}
]

RegionPlot3D[r3, PlotStyle -> [email protected]]

enter image description here

So you can play with translation and scaling with:

Sin[2 x Pi] > 0 || Sin[.5 (y + 1) Pi] > 0

enter image description here

$\endgroup$
4
  • 2
    $\begingroup$ Also works with SquareWave[x/2] > 0 || SquareWave[y/2] > 0, but the timings are horrible - 24s vs 0.4s for the Sin version. $\endgroup$
    – shrx
    Jan 24, 2016 at 8:26
  • $\begingroup$ The meshes are different too: i.stack.imgur.com/0WeFJ.png $\endgroup$
    – shrx
    Jan 24, 2016 at 8:33
  • $\begingroup$ @shrx Can't make that work on 10.3.1: ImplicitRegion[ SquareWave[x/2] > 0 || SquareWave[y/2] > 0, {{x, 0, 9}, {y, 0, 9}}] and the dense mesh you see for SquareWave is there for Sin appraoch. $\endgroup$
    – Kuba
    Jan 24, 2016 at 8:37
  • $\begingroup$ interesting, seems like a regression. I have 10.2.0 on OS X. $\endgroup$
    – shrx
    Jan 24, 2016 at 9:08
10
$\begingroup$

Here is an idea based on graphics primitives instead of mathematical inequalities.

columnWidth = 1;
regionSize = 10;
holes = Table[
   Rectangle[{x, y}, {x + columnWidth, y + columnWidth}],
   {x, columnWidth, regionSize, 2 columnWidth},
   {y, columnWidth, regionSize, 2 columnWidth}
   ];
holes // Graphics

Mathematica graphics

Now we subtract these squares from a larger square that covers all of the area:

RegionDifference[
  Rectangle[{0, 0}, {regionSize + columnWidth, regionSize + columnWidth}],
  RegionUnion[holes]
  ] // RegionPlot

Mathematica graphics

$\endgroup$
2
$\begingroup$

A bit late, but here's how the Mod[] version should have been implemented:

ir = ImplicitRegion[! (1 < Mod[x, 2] < 2 && 1 < Mod[y, 2] < 2), {{x, 0, 9}, {y, 0, 9}}];
RegionPlot[ir]

some grid

A 3D version, just like in Kuba's answer:

ir3 = ImplicitRegion[! (1 < Mod[x, 2] < 2 && 1 < Mod[y, 2] < 2 && 1 < Mod[z, 2] < 2),
                     {{x, 0, 9}, {y, 0, 9}, {z, 0, 9}}];
RegionPlot3D[ir3, PlotStyle -> Opacity[.5]]

a 3D grid


It's probably worth pointing out that I plan on using this region as the domain specification of a ParametricPlot3D.

In such a case, it is often more efficient to do preprocessing with (Boundary)DiscretizeRegion[] before using it as a plotting region.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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