4

Is it possible to extract the coordinates from each triangle after using TriangulateMesh? It sounds very weird but i really need it for one of my projects. I have to save each triangle separately in a txt file.

pts = {{-5, 29.6537}, {-4, 16.3031}, {-3, 13.8614}, {-2, 
    9.22332}, {-1, 6.89646}, {0, 6.76047}, {1, 9.20436}, {2, 
    6.65919}, {3, 18.2084}, {4, 18.9102}, {5, 31.6521}};

Show[TriangulateMesh[Polygon[pts], MaxCellMeasure -> {"Area" -> Infinity}], Axes -> True, AspectRatio -> 1/GoldenRatio] ยดยดยด

NeAr
  • 285
  • 1
  • 9

2 Answers2

4

You could do something like this:

tm = TriangulateMesh[Polygon[pts], 
   MaxCellMeasure -> {"Area" -> Infinity}];
crd = MeshCoordinates[tm];
incidents = MeshCells[tm, 2] /. Polygon[inc_] -> inc;
tris = crd[[#]] & /@ incidents;
tris[[1]]
(* {{-2., 9.22332}, {1., 9.20436}, {2.34505, 10.6443}} *)
Tim Laska
  • 16,346
  • 1
  • 34
  • 58
4
tm = TriangulateMesh[Polygon[pts], MaxCellMeasure -> {"Area" -> Infinity}];

You can also use MeshPrimitives to get the triangles and take first part of each to get the coordinates:

trianglecoords = MeshPrimitives[tm, 2][[All, 1]];

trianglecoords == tris (* tris from Tim Laska's answer *)

True
Graphics[{RandomColor[], #} & /@ MeshPrimitives[tm, 2]]

enter image description here

Graphics[{RandomColor[], Polygon@#} & /@ trianglecoords] 

enter image description here

kglr
  • 394,356
  • 18
  • 477
  • 896