NearestMeshCells[mr,pt]
gives the indices for the cells to which the point pt is nearest in the mesh region mr.
NearestMeshCells[mr,pt,n]
gives the n nearest cell indices to pt.
NearestMeshCells[mr,pt,{n,r}]
gives the n or fewer nearest cell indices to pt that are within radius r of pt.
NearestMeshCells[{mr,d},…]
gives the indices for the cells of dimension d.
NearestMeshCells
NearestMeshCells[mr,pt]
gives the indices for the cells to which the point pt is nearest in the mesh region mr.
NearestMeshCells[mr,pt,n]
gives the n nearest cell indices to pt.
NearestMeshCells[mr,pt,{n,r}]
gives the n or fewer nearest cell indices to pt that are within radius r of pt.
NearestMeshCells[{mr,d},…]
gives the indices for the cells of dimension d.
Details
- NearestMeshCells is also known as nearest point or nearest region.
- NearestMeshCells is typically used to find the closest cells in mesh from a given point.
- The distance to a cell in a mesh is the distance to the closest point in that cell. The nearest cell is the one with the smallest distance.
- NearestMeshCells returns a list of cell indexes of the form {d,{i1,i2,…}}.
Examples
open all close allBasic Examples (2)
Find the nearest line in the triangle:
NearestMeshCells[{Triangle[], 1}, {1, 1}]Show[HighlightMesh[Triangle[], %], Graphics[{Red, Point[{1, 1}]}]]Find the nearest cell in the mesh region:
ℛ = MeshRegion[{{0, 0, 0}, {1, 0, 0}, {2, 0, 0}, {3, 0, -1 / 2}, {3, 0, 1 / 2}, {4, -1 / 2, 0}, {4, 1 / 2, 0}}, {Point[1], Line[{2, 3}], Triangle[{3, 4, 5}], Tetrahedron[{4, 6, 5, 7}]}];NearestMeshCells[ℛ, {2, 1, 1}]Show[HighlightMesh[ℛ, %], Graphics3D[{Red, Point[{2, 1, 1}]}]]Scope (8)
Basic Uses (2)
Find the cell nearest to {2,1}:
ℛ = MeshRegion[{{0, 0}, {1, 0}, {2, 1 / 2}, {2, -1 / 2}}, {Line[{1, 2}], Triangle[{2, 3, 4}]}];NearestMeshCells[ℛ, {2, 1}]NearestMeshCells[{ℛ, 0}, {2, 1}]NearestMeshCells[{ℛ, 1}, {2, 1}]Find the 5 cells nearest to {1.5,.5}:
ℛ = VoronoiMesh[RandomReal[1, {50, 2}]];NearestMeshCells[ℛ, {1.5, .5}, 5]At most 10 cells within radius 0.5:
NearestMeshCells[ℛ, {1.5, .5}, {10, .5}]Mesh Regions (2)
Find the nearest cell in a MeshRegion:
ℛ = DelaunayMesh[RandomReal[1, {50, 3}]]NearestMeshCells[ℛ, {1, 0, 1}]Show[HighlightMesh[ℛ, %], Graphics3D[{Red, Point[{1, 0, 1}]}]]Find the nearest line in a BoundaryMeshRegion:
ℛ = BoundaryMeshRegion[{{0, 0}, {3, 0}, {3, 3}, {0, 3}, {1, 1}, {2, 1}, {2, 2}, {1, 2}}, Line[{1, 2, 3, 4, 1}], Line[{5, 6, 7, 8, 5}]]NearestMeshCells[{ℛ, 1}, {5, 1.5}]Show[HighlightMesh[ℛ, %], Graphics[{Red, Point[{5, 1.5}]}]]Polygons & Polyhedra (4)
ℛ = Polyhedron[{{-0.85065080835204, 0., 1.1135163644116066}, {-0.2628655560595668, 0.8090169943749475,
1.1135163644116066}, {-0.42532540417602, 1.3090169943749475, 0.2628655560595668},
{-1.1135163644116066, 0.8090169943749475, -0.262865556059566 ...
{{1, 2, 3, 4, 5}, {6, 7, 1, 5, 8}, {9, 10, 6, 8, 11}, {12, 13, 9, 11, 14}, {4, 14, 11, 8, 5},
{3, 15, 12, 14, 4}, {16, 17, 10, 9, 13}, {18, 16, 13, 12, 15}, {19, 18, 15, 3, 2},
{20, 19, 2, 1, 7}, {17, 20, 7, 6, 10}, {16, 18, 19, 20, 17}}];NearestMeshCells[ℛ, {1.5, 1, 1}]Show[HighlightMesh[ℛ, %], Graphics3D[{Red, Point[{1.5, 1, 1}]}]]ℛ = Polygon[{{{0, 0}, {1, 0}, {0, 1}}, {{1, 1}, {2, 1}, {1, 2}}}];NearestMeshCells[ℛ, {1, .8}]Show[HighlightMesh[ℛ, %], Graphics[{Red, Point[{1, .8}]}]]Line:
ℛ = Line[{{1, 0}, {2, 1}, {3, 0}, {4, 1}}];NearestMeshCells[Line[{{1, 0}, {2, 1}, {3, 0}, {4, 1}}], {1, 1}]Show[HighlightMesh[ℛ, %], Graphics[{Red, Point[{1, 1}]}]]ℛ = Point[Table[{t, Sin[t]}, {t, 0, 2π, 2π / 10}]];NearestMeshCells[ℛ, {1.2, .5}]Show[HighlightMesh[ℛ, %], Graphics[{Red, Point[{1.2, .5}]}]]Applications (1)
Find a Voronoi cell that corresponds to the given point:
pts = RandomReal[{-1, 1}, {25, 2}];vmesh = VoronoiMesh[pts]NearestMeshCells[vmesh, pts[[1]]]Show[HighlightMesh[vmesh, %], Graphics[{Orange, Point[pts[[1]]]}]]Interactive Examples (1)
Related Guides
History
Text
Wolfram Research (2020), NearestMeshCells, Wolfram Language function, https://reference.wolfram.com/language/ref/NearestMeshCells.html.
CMS
Wolfram Language. 2020. "NearestMeshCells." Wolfram Language & System Documentation Center. Wolfram Research. https://reference.wolfram.com/language/ref/NearestMeshCells.html.
APA
Wolfram Language. (2020). NearestMeshCells. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/NearestMeshCells.html
BibTeX
@misc{reference.wolfram_2026_nearestmeshcells, author="Wolfram Research", title="{NearestMeshCells}", year="2020", howpublished="\url{https://reference.wolfram.com/language/ref/NearestMeshCells.html}", note=[Accessed: 15-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_nearestmeshcells, organization={Wolfram Research}, title={NearestMeshCells}, year={2020}, url={https://reference.wolfram.com/language/ref/NearestMeshCells.html}, note=[Accessed: 15-June-2026]}