8
$\begingroup$

Here is the code I am working with. I create a list of random coordinates, then I create a triangular mesh with the code:

list = RandomInteger[{1, 10}, {10, 2}]
mesh = DelaunayMesh[
  list,
  MeshCellLabel -> {0 -> "Index"},
  PlotTheme -> "Lines",
  MeshCellHighlight -> {{1, All} -> Green, {0, All} -> Black},
  Frame -> True
  ]

enter image description here

How can I select a specific triangle? For example, how can I select the triangle with vertices {10,7,2}.

$\endgroup$
8
$\begingroup$

Update: A more convenient version that allows the vertex indices in any order:

ClearAll[vToF]
vToF[vl : {__}] := KeyMap[ Sort, vertexToFace][Sort[vl]];

HighlightMesh[mesh, {Style[{2, vToF[{10, 9, 4}]}, Red], 
  Style[{2, vToF[{8, 1, 5}]}, Yellow]}]

enter image description here

Original answer:

SeedRandom[1]
list = RandomInteger[{1, 10}, {10, 2}];
mesh = DelaunayMesh[list, MeshCellLabel -> {0 -> "Index"}, 
    PlotTheme -> "Lines", 
    MeshCellHighlight -> {{1, All} -> Green, {0, All} -> Black}, 
    Frame -> True]

You can create an association mapping face vertices to face indices using the property "FaceVertexConnectivityRules":

vertexToFace = Association[Reverse /@ mesh["FaceVertexConnectivityRules"]]

<|{3, 8, 5} -> 1, {8, 3, 7} -> 2, {2, 5, 10} -> 3, {1, 5, 8} -> 4, {10, 5, 1} -> 5, {8, 9, 1} -> 6, {9, 7, 4} -> 7, {7, 9, 8} -> 8, {10, 4, 6} -> 9, {4, 10, 9} -> 10, {10, 6, 2} -> 11, {9, 10, 1} -> 12|>

HighlightMesh[mesh, Style[{2, vertexToFace[{3, 8, 5}]}, Red]]

enter image description here

$\endgroup$
  • $\begingroup$ How did you know about the faces vertex coordinates rules? I couldn't find it in the documentation. $\endgroup$ – Las des Nestor Jul 3 at 15:53
  • $\begingroup$ @LasdesNestor Yes, these things are mostly undocumented. This is why Mathematica.StackExchange is so valuable: People try a lot of spelunking and also a few of the developers are around here. $\endgroup$ – Henrik Schumacher Jul 3 at 15:58
  • $\begingroup$ @LasdesNestor, try mesh["Properties"] to see a long list of properties. Since they are not documented it is by trial and luck that you discover quiet a few useful properties. $\endgroup$ – kglr Jul 3 at 16:00
  • $\begingroup$ Is there a way to extract that particular triangle, so I can compare them with the other ones? Let say that I select a triangle and I want to check if the triangle is part of the original plot. $\endgroup$ – Las des Nestor Jul 3 at 17:33
  • 1
    $\begingroup$ @LasdesNestor, the triangle (polygon) can be obtained using MeshPrimitives[mesh, {2,1}] or using MeshPrimitives[mesh, {2,vertexToFace[{3,8,5}]}]. $\endgroup$ – kglr Jul 3 at 18:06

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.