6
$\begingroup$

Given a set p of points (2D), each with a unique color c, I can mesh these points easily

p = RandomReal[{0, 1}, {10, 2}];
c = Map[Hue[#] &, RandomReal[{0, 1}, 10]]; 
Show[DelaunayMesh[p],Graphics[Graphics[MapThread[{PointSize[.05], #1, Point[#2]} &, {c, p}]]]]

enter image description here

My question:

How can I easily colorize the lines and/or triangles according to the neighboring node colors?

Thanks!

$\endgroup$
  • $\begingroup$ Can you clarify the rules? For instance, what color should the line between a blue and yellow dot be? How should a triangle face color be chosen, given the colors of its vertices? $\endgroup$ – MarcoB Mar 6 '19 at 18:52
  • $\begingroup$ @ MarcoB Along the line I would expect some blending. $\endgroup$ – Ulrich Neumann Mar 6 '19 at 18:55
7
$\begingroup$

Do you mean something like this?

R = DelaunayMesh[p];
Graphics[
 GraphicsComplex[
  MeshCoordinates[R],
  MeshCells[R, 2, "Multicells" -> True],
  VertexColors -> c
  ]
 ]

enter image description here

Edit

Getting colorgradients on the edges only seems to be somewhat trickier as VertexCoordinate for Line objects in a GraphicsComplex does not work. A workaround could be this:

elist = Flatten[MeshCells[R, 1, "Multicells" -> True][[1, 1]]];
Graphics[
 Line[
  Partition[MeshCoordinates[R][[elist]], 2],
  VertexColors -> Partition[c[[elist]], 2]
  ]
 ]

enter image description here

$\endgroup$
  • $\begingroup$ Yes, that's it! Thank you very much $\endgroup$ – Ulrich Neumann Mar 6 '19 at 18:58
  • $\begingroup$ You're welcome, Ulrich. $\endgroup$ – Henrik Schumacher Mar 6 '19 at 19:02
  • $\begingroup$ @ Henrik Where can I find information about the option "Multicells" -> True? In MMA v11.0.1. it isn't known . $\endgroup$ – Ulrich Neumann Mar 6 '19 at 19:28
  • 1
    $\begingroup$ Nowhere. user21 once told me. Very useful. Even the syntax highlighter of version 11.3 but it might work in version 11.0.1 already. If not, you may use Polygon[MeshCells[R, 2][[All, 1]]] instead (but it unpacks arrays and therefore may be slower for large meshes). $\endgroup$ – Henrik Schumacher Mar 6 '19 at 19:35
  • $\begingroup$ Again thank you for your useful help! $\endgroup$ – Ulrich Neumann Mar 6 '19 at 19:39
4
$\begingroup$
SeedRandom[1]
p = RandomReal[{0, 1}, {10, 2}];
c = Map[Hue[#] &, RandomReal[{0, 1}, 10]];

1. Post-process MeshPrimitives to add VertexColors:

rule = pr : (_Polygon | _Line | _Point) :> 
  Append[pr, VertexColors -> 
    AssociationThread[p, c] /@ First[pr /. Point[x_] :> Point[{x}]]]

Row[Graphics[{AbsoluteThickness[5], AbsolutePointSize[12], 
     MeshPrimitives[DelaunayMesh[p], #] /. rule,
     AbsoluteThickness[3], Black, Circle[#, Offset[6]] & /@ p}, 
    ImageSize -> 300, PlotLabel -> Style[#, 16, Black]] & /@
 {0 | 1, 0 | 2, 0 | 1 | 2}, Spacer[5]]

enter image description here

2. Use the option MeshCellShapeFunction with desired styles/primitives for vertices, edges and faces:

Row[{DelaunayMesh[p, ImageSize -> 400,
   MeshCellStyle -> White,
   MeshCellShapeFunction ->
    {{0, All} -> ({c[[#3[[1, 1]]]], Disk[#, Offset[7]]} &),
     {1, All} -> ({AbsoluteThickness[7], CapForm["Round"], 
         Line[#, VertexColors -> c[[#3[[1, 1]]]]]} &),
     {2, All} -> None}],
  DelaunayMesh[p, ImageSize -> 400,
   MeshCellStyle -> White,
   MeshCellShapeFunction ->
    {{0, All} -> ({c[[#3[[1, 1]]]], Disk[#, Offset[7]]} &),
     {1, All} -> None,
     {2, All} -> ({EdgeForm[Gray], 
         Polygon[#, VertexColors -> c[[#3[[1, 1]]]]]} &)}]}]

enter image description here

$\endgroup$
3
$\begingroup$

If you're interested in doing this with 3D mesh objects (as I was) the following works in Mathematica 12.1:

mesh = BoundaryDiscretizeRegion[Ball[{0, 0, 0}, 1], 
  MaxCellMeasure -> {"Length" -> 1}, PrecisionGoal -> 0.01] (* or any mesh object *)
vertexscalar = RandomReal[{0, 1}, Length[MeshCoordinates[mesh]]]

Graphics3D[
 GraphicsComplex[MeshCoordinates[mesh], 
  Polygon[MeshCells[mesh, 2][[All, 1]]], 
  VertexColors -> ColorData["DarkRainbow"] /@ Rescale[vertexscalar]]]

a spherical mesh with the verticies randomly colored

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