RegionNearestFunction[reg,…]
represents a function whose values give the nearest point in the region reg.
RegionNearestFunction
RegionNearestFunction[reg,…]
represents a function whose values give the nearest point in the region reg.
Details
- RegionNearestFunction works like Function.
- RegionNearestFunction is generated by RegionNearest[reg].
- RegionNearestFunction[reg,…][p] gives the point q∈reg that minimizes Norm[p-q].
Examples
open all close allBasic Examples (1)
Generate a region nearest function for a unit disk:
nf = RegionNearest[Disk[]]Apply the distance function to multiple points:
pts = Tuples[Range[-2, 2], 2];
Short[nf[pts]]Legended[Graphics[{{LightBlue, EdgeForm[Gray], Disk[]}, {Thin, Gray, Line[Transpose[{pts, nf[pts]}]]}, {Red, Point[pts]}, {Blue, Point[nf[pts]]}}], PointLegend[{Red, Blue}, {"start", "nearest"}]]Scope (16)
Special Regions (7)
Point sets:
RegionNearest[Point[{{1, 1}, {1, 2}}]][{3, 4}]RegionNearest[Point[{{0, 0, 0, 0}, {1, 2, 3, 4}}]][{2, 2, 2, 2}]ℛ = Point[{{1, 2}, {3, 4}, {5, 6}}];
pts = Table[{3, 4} + 4{Cos[k 2π / 16], Sin[k 2π / 16]}, {k, 0, 15}];
nf = RegionNearest[ℛ];Legended[Graphics[{ℛ, {Thin, Gray, Line[Transpose[{pts, nf[pts]}]]}, {Red, Point[pts]}, {Blue, Point[nf[pts]]}}], PointLegend[{Red, Blue}, {"start", "nearest"}]]Line:
RegionNearest[Line[{{0, 0}, {1, 1}}]][{3, 4}]RegionNearest[Line[{{0, 0, 0}, {1, 1, 1}, {0, 0, 1}}]][{1, 0, 3}]ℛ = Line[{{0, 0}, {1, 2}, {1, 0}, {2, 2}}];
pts = Table[{1, 1} + 2{Cos[k 2π / 16], Sin[k 2π / 16]}, {k, 0, 15}];
nf = RegionNearest[ℛ];Legended[Graphics[{ℛ, {Thin, Gray, Line[Transpose[{pts, nf[pts]}]]}, {Red, Point[pts]}, {Blue, Point[nf[pts]]}}], PointLegend[{Red, Blue}, {"start", "nearest"}]]Rectangle can be used in 2D:
RegionNearest[Rectangle[{0, 0}, {1, 2}]][{3, 4}]Cuboid generalizes it to any dimension:
RegionNearest[Cuboid[{0, 0, 0, 0}, {1, 2, 3, 4}]][{1, 1, 1, 5}]Visualize the nearest points on a rectangle:
ℛ = Rectangle[{0, 0}, {2, 1}];
pts = Table[{1, 1 / 2} + 2{Cos[k 2π / 16], Sin[k 2π / 16]}, {k, 0, 15}];
nf = RegionNearest[ℛ];Legended[Graphics[{{LightBlue, EdgeForm[Gray], ℛ}, {Thin, Gray, Line[Transpose[{pts, nf[pts]}]]}, {Red, Point[pts]}, {Blue, Point[nf[pts]]}}], PointLegend[{Red, Blue}, {"start", "nearest"}]]RegionNearest[Simplex[1]][{2}]RegionNearest[Simplex[3]][{1, 1, 1}]Visualize the nearest points on a 2D simplex:
ℛ = Simplex[2];
pts = Table[{1 / 3, 1 / 3} + {Cos[k 2π / 16], Sin[k 2π / 16]}, {k, 0, 15}];
nf = RegionNearest[ℛ];Legended[Graphics[{{LightBlue, EdgeForm[Gray], ℛ}, {Thin, Gray, Line[Transpose[{pts, nf[pts]}]]}, {Red, Point[pts]}, {Blue, Point[nf[pts]]}}], PointLegend[{Red, Blue}, {"start", "nearest"}]]RegionNearest[Polygon[{{0, 0}, {1, 0}, {0, 1}}]][{1, 1}]Visualize the nearest points in a hexagon:
ℛ = Polygon[{{-2, 0}, {-1, 3 / 2}, {1, 3 / 2}, {2, 0}, {1, -3 / 2}, {-1, -3 / 2}}];
pts = Table[4{Cos[k 2 π / 25], Sin[k 2π / 25]}, {k, 0., 24}];
nf = RegionNearest[ℛ];Legended[Graphics[{{LightBlue, EdgeForm[Gray], ℛ}, {Thin, Gray, Line[Transpose[{pts, nf[pts]}]]}, {Red, Point[pts]}, {Blue, Point[nf[pts]]}}], PointLegend[{Red, Blue}, {"start", "nearest"}]]Disk can be used in 2D:
RegionNearest[Disk[{0, 0}, 1]][{3, 4}]Ball generalizes a disk in any dimension:
RegionNearest[Ball[{0, 0, 0}, 1]][{3, 4, 5}]Visualize the nearest points on a 3D ball:
ℛ = Ball[{0, 0, 0}, 1];
pts = Flatten[Table[2{Cos[k 2π / 8]Cos[l π / 8], Sin[k 2π / 8]Cos[l π / 8], Sin[l π / 8]}, {k, 0, 7}, {l, -3, 3}], 1];
nf = RegionNearest[ℛ];Legended[Graphics3D[{{LightBlue, EdgeForm[Gray], ℛ}, {Thin, Gray, Line[Transpose[{pts, nf[pts]}]]}, {Red, Point[pts]}, {Blue, Point[nf[pts]]}}], PointLegend[{Red, Blue}, {"start", "nearest"}]]Disk can be an ellipse in 2D:
RegionNearest[Disk[{0, 0}, {1, 2}]][{0, 4}]Ellipsoid generalizes ellipses to any dimension:
RegionNearest[Ellipsoid[{0, 0, 0}, {1, 2, 3}]][{0, 3, 4}]Plot the nearest points on an ellipse:
ℛ = Disk[{0, 0}, {2, 1}];
pts = Table[3{Cos[k 2π / 16], Sin[k 2π / 16]}, {k, 0., 15}];
nf = RegionNearest[ℛ];Legended[Graphics[{{LightBlue, EdgeForm[Gray], ℛ}, {Thin, Gray, Line[Transpose[{pts, nf[pts]}]]}, {Red, Point[pts]}, {Blue, Point[nf[pts]]}}], PointLegend[{Red, Blue}, {"start", "nearest"}]]Formula Regions (2)
The nearest point on a disk represented as an ImplicitRegion:
ℛ = ImplicitRegion[x^2 + y^2 ≤ 1, {x, y}];Region[ℛ]nf = RegionNearest[ℛ]nf /@ RandomReal[1, {5, 2}]ℛ = ImplicitRegion[x^2 + y^2 ≤ 1, {x, y, {z, 0, 2}}];RegionNearest[ℛ][{3, 4, 5}]The nearest point on a disk represented as a ParametricRegion:
ℛ = ParametricRegion[{r Cos[θ], r Sin[θ]}, {{r, 0, 1}, {θ, 0, 2π}}];nf = RegionNearest[ℛ]nf /@ RandomReal[1, {5, 2}]Using a rational parametrization of the disk:
ℛ = ParametricRegion[{r(1 - t^2/1 + t^2), r(2t/1 + t^2)}, {t, {r, 0, 1}}];RegionNearest[ℛ][{3, 4}]ℛ = ParametricRegion[{r Cos[θ], r Sin[θ], z}, {{r, 0, 1}, {θ, 0, 2π}, {z, 0, 2}}];RegionNearest[ℛ][{3, 4, 5}]FullSimplify[%]Mesh Regions (4)
The nearest point in a 1D BoundaryMeshRegion:
ℛ = BoundaryMeshRegion[{{0}, {1}}, Point[{{1}, {2}}]]RegionNearest[ℛ][{2}]ℛ = ConvexHullMesh[RandomReal[1, {10, 2}]];Show[ℛ, Graphics[{Red, Point[{1, 1}]}]]RegionNearest[ℛ][{1, 1}]ℛ = ConvexHullMesh[RandomReal[1, {20, 3}]];Show[ℛ, Graphics3D[{Red, Point[{1, 1, 1}]}]]RegionNearest[ℛ][{1, 1, 1}]The nearest point in a 0D MeshRegion in 1D:
ℛ = MeshRegion[{{0}, {1}}, Point[{{1}, {2}}]]RegionNearest[ℛ][{1 / 2}]ℛ = MeshRegion[{{0}, {1}}, Line[{1, 2}]]RegionNearest[ℛ][{2}]The nearest point in a 0D MeshRegion in 2D:
ℛ = MeshRegion[{{0, 0}, {1, 0}, {0, 1}}, Point[{{1}, {2}, {3}}]];Show[ℛ, Graphics[{Red, Point[{1, 1}]}]]RegionNearest[ℛ][{1, 1}]ℛ = MeshRegion[{{0, 0}, {1, 0}, {0, 1}}, Line[{1, 2, 3, 1}]];Show[ℛ, Graphics[{Red, Point[{1, 1}]}]]RegionNearest[ℛ][{1, 1}]ℛ = MeshRegion[{{0, 0}, {1, 0}, {0, 1}}, Polygon[{1, 2, 3}]];Show[ℛ, Graphics[{Red, Point[{1, 1}]}]]RegionNearest[ℛ][{1, 1}]The nearest point in a 0D MeshRegion in 3D:
ℛ = MeshRegion[{{0, 0, 0}, {0, 0, 1}, {0, 1, 0}, {1, 0, 0}}, Point[{{1}, {2}, {3}, {4}}]];Show[ℛ, Graphics3D[{Red, Point[{1, 1, 1}]}]]RegionNearest[ℛ][{1, 1, 1}]ℛ = MeshRegion[{{0, 0, 0}, {0, 0, 1}, {0, 1, 0}, {1, 0, 0}}, Line[{{1, 2, 3, 4, 1}}]];Show[ℛ, Graphics3D[{Red, Point[{1, 1, 1}]}]]RegionNearest[ℛ][{1, 1, 1}]ℛ = MeshRegion[{{0, 0, 0}, {0, 0, 1}, {0, 1, 0}, {1, 0, 0}}, Polygon[{{1, 2, 3}, {2, 3, 4}}]];Show[ℛ, Graphics3D[{Red, Point[{1, 1, 1}]}]]RegionNearest[ℛ][{1, 1, 1}]ℛ = MeshRegion[{{0, 0, 0}, {0, 0, 1}, {0, 1, 0}, {1, 0, 0}}, Tetrahedron[{{1, 2, 3, 4}}]];Show[ℛ, Graphics3D[{Red, Point[{1, 1, 1}]}]]RegionNearest[ℛ][{1, 1, 1}]Derived Regions (3)
The distance to a RegionIntersection:
ℛ = RegionIntersection[Disk[{0, 0}, 1], Disk[{0, 1}, 1]];Show[Graphics[{Red, Point[{1, 1}]}], Region[ℛ]]RegionNearest[ℛ][{1, 1}]The distance to a TransformedRegion:
ℛ = TransformedRegion[Disk[], ScalingTransform[{3, 2}]];Show[Region[ℛ], Graphics[{Red, Point[{4, 3}]}]]RegionNearest[ℛ][{4, 3}]The distance to a RegionBoundary:
ℛ = RegionBoundary[Disk[]];Graphics[{ℛ, Red, Point[{1 / 2, 1 / 2}]}]RegionNearest[ℛ][{1 / 2, 1 / 2}]Applications (2)
Visualize the nearest points in a region:
ℛ = BoundaryDiscretizeRegion[ImplicitRegion[Abs[x]^1 / 2 + Abs[y]^1 / 2 ≤ 1, {x, y}], {{-1, 1}, {-1, 1}}];
nf = RegionNearest[ℛ];
pts = Flatten[Table[r{Cos[k 2π / 128], Sin[k 2π / 128]}, {k, 0, 127}, {r, 1, 2}], 1];
nst = nf[pts];Show[ℛ, Graphics[{Thin, Gray, Line[Transpose[{pts, nst}]]}]]Use Locator to find the nearest point to a country region:
ℛ = DiscretizeGraphics[CountryData["Italy", "Shape"]];nf = RegionNearest[ℛ]DynamicModule[{p = {0.05, 0.05}}, Show[ℛ, Graphics[{Locator[Dynamic[p]], Red, Dynamic@Line[{nf[p], p}]}, PlotRange -> RegionBounds[ℛ]]], SaveDefinitions -> True]Properties & Relations (2)
RegionNearestFunction gives the same result when applied to a point as RegionNearest[r,p]:
ℛ = Triangle[];
nf = RegionNearest[ℛ]pts = RandomReal[1, {100, 2}];nf[pts] == RegionNearest[ℛ, pts]Formulas returned by RegionNearest, evaluated at a point, coincide with RegionNearestFunction:
ℛ = Circle[];
nf = RegionNearest[ℛ]pts = RandomReal[1, {100, 2}];f[{x_, y_}] = RegionNearest[ℛ, {x, y}]nf[pts] == f /@ ptsPossible Issues (1)
RegionNearestFunction can only be generated for ConstantRegionQ regions:
c = Circle[{0, 0}, r];ConstantRegionQ[c]RegionNearest[c]Related Guides
History
Text
Wolfram Research (2014), RegionNearestFunction, Wolfram Language function, https://reference.wolfram.com/language/ref/RegionNearestFunction.html.
CMS
Wolfram Language. 2014. "RegionNearestFunction." Wolfram Language & System Documentation Center. Wolfram Research. https://reference.wolfram.com/language/ref/RegionNearestFunction.html.
APA
Wolfram Language. (2014). RegionNearestFunction. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/RegionNearestFunction.html
BibTeX
@misc{reference.wolfram_2026_regionnearestfunction, author="Wolfram Research", title="{RegionNearestFunction}", year="2014", howpublished="\url{https://reference.wolfram.com/language/ref/RegionNearestFunction.html}", note=[Accessed: 13-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_regionnearestfunction, organization={Wolfram Research}, title={RegionNearestFunction}, year={2014}, url={https://reference.wolfram.com/language/ref/RegionNearestFunction.html}, note=[Accessed: 13-June-2026]}