6
$\begingroup$

This question already has an answer here:

Given a set of points

pts = Table[{RandomReal[], RandomReal[]}, {i, 1, 10}];

and their Voronoi diagram

vor = VoronoiMesh[pts];

I can list the 2-dimensional cells as follows:

cells=MeshCells[vor, 2];

Given an element from this list, say

cells[[5]]

How do I recover the coordinates of the point from pts that is contained in that cell?

$\endgroup$

marked as duplicate by m_goldberg, MarcoB, Alex Trounev, Thies Heidecke, MikeLimaOscar Jun 21 at 16:22

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

7
$\begingroup$

You can use Region`Mesh`MeshNearestCellIndex to construct Associations mapping faces to points and points to faces

faceToPoint = Association[Region`Mesh`MeshNearestCellIndex[vor, #] -> # & /@ pts]

{{2, 5} -> {0.817389, 0.11142}, {2, 1} -> {0.789526, 0.187803}, {2,    7} -> {0.241361, 0.0657388}, {2, 9} -> {0.542247, 0.231155}, {2,    10} -> {0.396006, 0.700474}, {2, 2} -> {0.211826, 0.748657}, {2,    3} -> {0.422851, 0.247495}, {2, 4} -> {0.977172, 0.825163}, {2, 8} -> {0.925275, 0.578056}, {2, 6} -> {0.29287, 0.208051}}

pointToFace = Association[# -> Region`Mesh`MeshNearestCellIndex[vor, #] & /@ pts]

<|{0.817389, 0.11142} -> {2, 5}, {0.789526, 0.187803} -> {2,    1}, {0.241361, 0.0657388} -> {2, 7}, {0.542247, 0.231155} -> {2,    9}, {0.396006, 0.700474} -> {2, 10}, {0.211826, 0.748657} -> {2,    2}, {0.422851, 0.247495} -> {2, 3}, {0.977172, 0.825163} -> {2,    4}, {0.925275, 0.578056} -> {2, 8}, {0.29287, 0.208051} -> {2, 6}|>

k = 5;
Show[HighlightMesh[vor, {2, k}], 
 Graphics[{Red, PointSize[Large], Point @ faceToPoint[{2, k}]}]]

enter image description here

For k = 8 we get

enter image description here

$\endgroup$

Not the answer you're looking for? Browse other questions tagged or ask your own question.