3
$\begingroup$

I have a polygon and 3 lists of points (H, M, L) inside this polygon. I would like to create a mesh inside this polygon and to colorcode each tile of the mesh depending on how many H, M and L there are inside.

I think I can use RGB to define the color based on the number of H, M and L but I'm clueless on how to specify the mesh and to define if a point belong to a tile rather than another.

Any suggestions? I'm adding a little example:

Graphics[{EdgeForm[Thick], White,Polygon[{{-0.5, 5.5}, {10.5, 5.5}, {-0.50,-0.50}}]}];
H = {{0, 0}, {0, 1}, {0, 3}, {1, 1}, {1, 2}, {0, 5}, {0.5, 2}, {0.5, 4}, {0.5, 5}, {3, 5}};
M = {{1, 1.5}, {2, 1.5}, {5, 3.5}, {4, 2.5}, {2, 4}, {2, 1}, {3, 3}, {6, 4.5}, {6, 4}, {8, 5}};
L = {{4, 3.5}, {0.5, 1}, {2, 3}, {5, 4}, {4, 5}, {7, 5}, {7, 4}, {6, 3.5}, {8, 4.5}, {9, 5}};

enter image description here

$\endgroup$
  • $\begingroup$ It would be helpful if you post the code for the polygon and the points so one can visualize what you have in mind. $\endgroup$ – corey979 Jan 2 '17 at 9:46
  • $\begingroup$ Unfortunately they derive from experiments so they may change (Irregular polygon defined just by segments). I'm trying to be as general as possible, I can in case provide one test with a simple triangle and few points. $\endgroup$ – Yyrkoon Jan 2 '17 at 9:48
  • $\begingroup$ Please post the triangle and the points. $\endgroup$ – corey979 Jan 2 '17 at 9:50
  • $\begingroup$ Thanks so much for your kindness, just added some points and their representation. $\endgroup$ – Yyrkoon Jan 2 '17 at 10:25
  • $\begingroup$ "Mesh" is a bit vague here. Do you want a regular grid, a triangulation... which one? $\endgroup$ – J. M.'s technical difficulties Jan 2 '17 at 10:37
3
$\begingroup$

A related thread about coloring a mesh.


How I understand the question:

  1. to make some, e.g. triangular, mesh of the region;
  2. count the number of points from the three categories in each of the mesh cells;
  3. define a color based on the counts;
  4. color the cell with that color.

I decided to use the relative counts to define an RGBColor (see also here).


mesh = TriangulateMesh[#, MaxCellMeasure -> 10] & @
  BoundaryMeshRegion[{{-0.5, 5.5}, {10.5, 5.5}, {-0.50, -0.50}}, 
   Line[{1, 2, 3, 1}]]

enter image description here

(For arbitrary points pts forming the vertices of the polygon: BoundaryMeshRegion[pts, Line[Range@Length@pts~Join~{1}]]; one might want to play with MaxCellMeasure too.)

The cells are extracted with

cells = MeshPrimitives[mesh, 2]
cells // Short

{Polygon[{{-0.5,5.5},{1.25,2.5},{3.,5.5}}],<<4>>,Polygon[<<1>>]}

The indices of the faces:

ind = MeshCellIndex[mesh, 2]

{{2, 1}, {2, 2}, {2, 3}, {2, 4}, {2, 5}, {2, 6}}

Then the counts, colors and styling:

counts = Table[ (Count[RegionMember[cells[[i]], #] & /@ #, True]/Length[#]) & /@ 
           {H, M, L}, {i, 1, Length @ cells}];

color = RGBColor /@ counts

style = Style @@@ (Transpose@{ind, color})

And coloring the mesh:

HighlightMesh[mesh, {style}]

enter image description here


To place the relative H-M-L counts on mesh:

lab = Transpose @ {ind, Text /@ counts};
HighlightMesh[mesh, Labeled[#1, #2] & @@@ lab]

enter image description here

On the colored mesh:

HighlightMesh[mesh, {style, Labeled[#1, #2] & @@@ lab}]

enter image description here

although it's not very readable; one might consider white text and some non-white background.

| improve this answer | |
$\endgroup$
  • $\begingroup$ I used RGB based on the question, but for readability it might be better to assign a color to each class (Red, Blue, Green) and use the most abundant group to define the lightness of the color that is ascribed to it; sth like Darker[Red, 3/10]; maybe some rescaling is needed also. $\endgroup$ – corey979 Jan 2 '17 at 12:05
  • $\begingroup$ Thank you very much for the reply. It's very clear and helpful $\endgroup$ – Yyrkoon Jan 2 '17 at 18:23

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.