# Compute Nearest Points

For a point and a region, the nearest point in that region to the given point can be computed. The nearest point is given by ArgMin[, ], where is the given point and is the region.

Some regions have simple closed-form representation for the nearest point.

 In[1]:= XRegionNearest[Disk[], {x, y}]
 Out[1]=
 In[2]:= XRegionNearest[Rectangle[], {x, y}]
 Out[2]=

Others are more involved.

 In[3]:= XRegionNearest[Triangle[{{0, 0}, {1, 0}, {0, 1}}], {x, y}]
 Out[3]=

Visualize the different case where there is a different nearest projector function.

 In[4]:= XpiecewiseConditions[HoldPattern[Piecewise[cases_, default_]]] := Module[{cl = Last /@ cases}, Append[cl, And @@ (Not /@ cl)]]
 In[5]:= Xconds = piecewiseConditions[RegionNearest[Triangle[], {x, y}]];
 In[6]:= XRegionPlot[conds, {x, -1, 2}, {y, -1, 2}, ImageSize -> Medium]
 Out[6]=

Similarly for a standard tetrahedron.

 In[7]:= XRegionNearest[Tetrahedron[], {x, y, z}]
 Out[7]=
 In[8]:= Xconds = piecewiseConditions[RegionNearest[Tetrahedron[], {x, y, z}]];
 In[9]:= XShow[Table[ RegionPlot3D[conds[[i]], {x, -1, 2}, {y, -1, 2}, {z, -1, 2}, Mesh -> None, PlotStyle -> Directive[Opacity[0.4], ColorData[95, i]], BoundaryStyle -> Gray, PlotPoints -> 35], {i, Length[conds]}], ImageSize -> Medium, Lighting -> "Neutral"]
 Out[9]=

## Mathematica

Questions? Comments? Contact a Wolfram expert »