2
$\begingroup$

So I created a mesh like this:

mesh2 = ToElementMesh[FullRegion[2], {{1, 10}, {1, 10}}, MaxCellMeasure -> 1.];

and want to plot it, coloring the squares:

Show[Graphics[ElementMeshToGraphicsComplex[mesh2, All,VertexColors -> {Red, Green, Red}]], mesh2["Wireframe"]]

What I get is this: Weirdly colored squares.

Now what I'd like is to color my squares homogeneously, i.e. I don't want the fancy fading effect from one square to the other, all I need is simple, even colors.

Thanks!

$\endgroup$
3
$\begingroup$

Some cheating necessary:

<< NDSolve`FEM`
mesh = ToElementMesh[FullRegion[2], {{1, 10}, {1, 10}}, MaxCellMeasure -> 1.];
Show[Graphics[Riffle[Flatten[Normal[ElementMeshToGraphicsComplex[mesh, All]]],
                     {Red, Green}, {1, -1, 2}]], mesh["Wireframe"]]

some Christmas-y checkerboard

$\endgroup$
  • $\begingroup$ Thanks! Could you please add a little explanation about what's going on? Especially with the Riffle[]... $\endgroup$ – botond Oct 20 '15 at 17:24
  • 2
    $\begingroup$ Normal[] is the usual way to convert a GraphicsComplex[] object into a list of graphics primitives with explicit coordinates. As for the Riffle[] bit: ponder on the result of Riffle[Range[7], {Red, Green}, {1, -1, 2}]. $\endgroup$ – J. M. will be back soon Oct 20 '15 at 17:31
2
$\begingroup$

Add markers to the QuadElement of the mesh, a list alternating 1/2 equal in length to the number of elements (vis Mod[Range@Length@q, 2, 1]):

Needs["NDSolve`FEM`"];    
mesh2 = ToElementMesh[FullRegion[2], {{1, 10}, {1, 10}}, MaxCellMeasure -> 1.] /. 
   QuadElement[q_] :> QuadElement[q, Mod[Range@Length@q, 2, 1]];

Then we can color each of the two markers independently:

mesh2["Wireframe"["MeshElementStyle" -> {FaceForm[Red], FaceForm[Green]}]]

Mathematica graphics

Like J.M.'s answer, it assumes that the elements come in a certain order. The order can be inspected as follows. The following shows the order of the elements:

 mesh2["Wireframe"["MeshElementIDStyle" -> Red]]

The following shows the markers:

mesh2["Wireframe"["MeshElementMarkerStyle" -> Blue]]

See the tutorial Element Mesh Visualization for more on visualizing meshes.

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