({{, (RefLink[RegionDistanceFunction, paclet:ref/RegionDistanceFunction])[reg,…] 
represents a function whose values give the distance from a point to the region reg.}})

OpenersNotesSection(Spacer1)Details

(RefLink[RegionDistanceFunction, paclet:ref/RegionDistanceFunction]) is also known as distance function, distance transform, signed distance function and signed distance transform.

(RefLink[RegionDistanceFunction, paclet:ref/RegionDistanceFunction]) works like (RefLink[Function, paclet:ref/Function]).

(RefLink[RegionDistanceFunction, paclet:ref/RegionDistanceFunction]) is generated by (RefLink[RegionDistance, paclet:ref/RegionDistance])[reg] or (RefLink[SignedRegionDistance, paclet:ref/SignedRegionDistance])[reg].

If generated by (RefLink[RegionDistance, paclet:ref/RegionDistance]), (RefLink[RegionDistanceFunction, paclet:ref/RegionDistanceFunction])[reg,…][p] gives (RefLink[RegionDistance, paclet:ref/RegionDistance])[reg,p].

If generated by (RefLink[SignedRegionDistance, paclet:ref/SignedRegionDistance]), (RefLink[RegionDistanceFunction, paclet:ref/RegionDistanceFunction])[reg,…][p] gives (RefLink[SignedRegionDistance, paclet:ref/SignedRegionDistance])[reg,p].

OpenersPrimaryExamplesSection(Spacer1)Examples  (22)

OpenersExampleSection0(Spacer1)Basic Examples  (1)

Generate a region distance function for a unit disk:

df=RegionDistance[Disk[]]

Apply the distance function to multiple points:

df/@RandomReal[{-2,2},{10,2}]

Plot it:

Plot3D[df[{x,y}],{x,-2,2},{y,-2,2}]

OpenersExampleSection1(Spacer1)Scope  (17)

OpenersExampleSubsection0(Spacer1)Special Regions  (8)

(RefLink[Point, paclet:ref/Point]) sets:

RegionDistance[Point[{{1,1},{1,2}}]][{3,4}]

RegionDistance[Point[{{0,0,0},{1,2,3}}]][{1,1,1}]

Plot the distance from a three-point set:

ℛ=Point[{{1,2},{3,4},{5,6}}];
df=RegionDistance[ℛ];

{Plot3D[df[{x,y}],{x,0,7},{y,0,7},MeshFunctions->{#3&},Mesh->5],ContourPlot[df[{x,y}],{x,-1,7},{y,0,8},Contours->{{0.5,Red},{1,Green},{1.5,Blue}}]}

(RefLink[Line, paclet:ref/Line]):

RegionDistance[Line[{{0,0},{1,1}}]][{3,4}]

RegionDistance[Line[{{0,0,0},{1,1,1},{0,0,1}}]][{1,2,3}]

Plot it:

ℛ=Line[{{0,0},{1,1}}];
df=RegionDistance[ℛ];

{Plot3D[df[{x,y}],{x,-1,2},{y,-1,2},MeshFunctions->{#3&},Mesh->5],ContourPlot[df[{x,y}],{x,-1,2},{y,-1,2},Contours->{{0.5,Red},{1,Green},{1.5,Blue}}]}

(RefLink[Rectangle, paclet:ref/Rectangle]) can be used in 2D:

RegionDistance[Rectangle[{0,0},{1,2}]][{3,4}]

(RefLink[Cuboid, paclet:ref/Cuboid]) generalizes it to any dimension:

RegionDistance[Cuboid[{0,0,0},{1,2,3}]][{4,5,6}]

Equidistance surfaces of a cuboid in 3D:

ℛ=Cuboid[{0,0,0},{1,1,1}];
df=RegionDistance[ℛ];

ContourPlot3D[Evaluate@df[{x,y,z}],{x,-1.1,2.1},{y,-1.1,2.1},{z,-1.1,2.1},Contours->{0.25,0.5,1.0},BaseStyle->Opacity[0.5],Mesh->None]

(RefLink[Simplex, paclet:ref/Simplex]) can be used in any dimension:

RegionDistance[Simplex[1]][{2}]

RegionDistance[Simplex[2]][{1,1}]

RegionDistance[Simplex[3]][{1,1,1}]

Plot the distance to a 2D simplex:

ℛ=Simplex[2];
df=RegionDistance[ℛ];

{Plot3D[df[{x,y}],{x,-1,2},{y,-1,2},MeshFunctions->{#3&},Mesh->5],ContourPlot[df[{x,y}],{x,-2,3},{y,-2,3},Contours->{{0.5,Red},{1,Green},{1.5,Blue}}]}

(RefLink[Disk, paclet:ref/Disk]) can be used in 2D:

RegionDistance[Disk[{0,0},1]][{1,2}]

(RefLink[Ball, paclet:ref/Ball]) generalizes a disk to any dimension:

RegionDistance[Ball[{0,0,0},1]][{1,2,3}]

Plot the distance to a disk:

ℛ=Disk[];
df=RegionDistance[ℛ];

{Plot3D[df[{x,y}],{x,-2,2},{y,-2,2},MeshFunctions->{#3&},Mesh->5],ContourPlot[df[{x,y}],{x,-3,3},{y,-3,3},Contours->{{0.5,Red},{1,Green},{1.5,Blue}}]}

(RefLink[Disk, paclet:ref/Disk]) can be an ellipse in 2D:

RegionDistance[Disk[{0,0},{1,2}]][{0,4}]

(RefLink[Ellipsoid, paclet:ref/Ellipsoid]) generalizes this to any dimension:

RegionDistance[Ellipsoid[{0,0,0},{1,2,3}]][{0,3,4}]

Plot the distance to an ellipse:

ℛ=Disk[{0,0},{1,2}];
df=RegionDistance[ℛ];

{Plot3D[Evaluate@df[{x,y}],{x,-2,2},{y,-3,3},MeshFunctions->{#3&},Mesh->5,Exclusions->Norm[{x,y/2}]==1],ContourPlot[Evaluate@df[{x,y}],{x,-3,3},{y,-4,4},Contours->{{0.5,Red},{1,Green},{1.5,Blue}},Exclusions->None]}

(RefLink[Circle, paclet:ref/Circle]) can be elliptical:

RegionDistance[Circle[{0,0},{2,1}]][{4,0}]

Plot the distance to a circle:

ℛ=Circle[];
df=RegionDistance[ℛ];

{Plot3D[df[{x,y}],{x,-2,2},{y,-2,2},MeshFunctions->{#3&},Mesh->5,Exclusions->Norm[{x,y}]==1],ContourPlot[df[{x,y}],{x,-3,3},{y,-3,3},Contours->{{0.5,Red},{1,Green},{1.5,Blue}}]}

(RefLink[Cylinder, paclet:ref/Cylinder]) and (RefLink[Cone, paclet:ref/Cone]):

RegionDistance[Cylinder[{{0,0,0},{0,0,2}},1]][{1,1,1}]//Simplify

RegionDistance[Cone[{{0,0,0},{0,0,2}},1]][{1,1,1}]//Simplify

Equidistance contours for a cylinder:

ℛ=Cylinder[{{0,0,0},{0,0,2}}];
df=RegionDistance[ℛ];

ContourPlot3D[Evaluate@df[{x,y,z}],{x,-3,3},{y,-3,3},{z,-2,4},Contours->{0.25,0.5,1.0},BaseStyle->Opacity[0.5],Mesh->None]

OpenersExampleSubsection1(Spacer1)Formula Regions  (2)

The distance to a disk represented as an (RefLink[ImplicitRegion, paclet:ref/ImplicitRegion]):

ℛ=ImplicitRegion[(x^2)+(y^2)≤1,{x,y}];

df=RegionDistance[ℛ];

{Plot3D[Evaluate@df[{x,y}],{x,-2,2},{y,-2,2},MeshFunctions->{#3&},Mesh->5,Exclusions->Norm[{x,y}]==1],ContourPlot[Evaluate@df[{x,y}],{x,-3,3},{y,-3,3},Contours->{{0.5,Red},{1,Green},{1.5,Blue}},Exclusions->None]}

df[RandomReal[{-2,2},{20,2}]]

A cylinder:

ℛ=ImplicitRegion[(x^2)+(y^2)≤1,{x,y,{z,0,2}}];

df=RegionDistance[ℛ];

df[RandomReal[{-2,2},{20,3}]]

The distance to a disk represented as a (RefLink[ParametricRegion, paclet:ref/ParametricRegion]):

ℛ=ParametricRegion[{r Cos[θ],r Sin[θ]},{{r,0,1},{θ,0,2π}}];

df=RegionDistance[ℛ];

df[RandomReal[{-2,2},{20,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}}];

df=RegionDistance[ℛ];

df[RandomReal[{-2,2},{20,2}]]

OpenersExampleSubsection2(Spacer1)Mesh Regions  (4)

The distance to a 1D (RefLink[BoundaryMeshRegion, paclet:ref/BoundaryMeshRegion]):

ℛ=BoundaryMeshRegion[{{0},{1}},Point[{{1},{2}}]]

df=RegionDistance[ℛ];
df[{2}]

2D:

ℛ=ConvexHullMesh[RandomReal[1,{10,2}]];

df=RegionDistance[ℛ];
{Plot3D[df[{x,y}],{x,-1,2},{y,-1,2},MeshFunctions->{#3&},Mesh->5],ContourPlot[df[{x,y}],{x,-2,3},{y,-2,3},Contours->{{0.5,Red},{1,Green},{1.5,Blue}}]}

The distance to a 0D (RefLink[MeshRegion, paclet:ref/MeshRegion]) in 1D:

ℛ=MeshRegion[{{0},{1}},Point[{{1},{2}}]]

df=RegionDistance[ℛ];
df[{1/2}]

A 1D region:

ℛ=MeshRegion[{{0},{1}},Line[{1,2}]]

df=RegionDistance[ℛ];
df[{2}]

The distance to a 0D (RefLink[MeshRegion, paclet:ref/MeshRegion]) in 2D:

ℛ=MeshRegion[{{0,0},{1,0},{0,1}},Point[{{1},{2},{3}}]];

Show[ℛ,Graphics[{Red,Point[{1,1}]}]]

df=RegionDistance[ℛ];
df[{1,1}]

A 1D region:

ℛ=MeshRegion[{{0,0},{1,0},{0,1}},Line[{1,2,3,1}]];

Show[ℛ,Graphics[{Red,Point[{1,1}]}]]

df=RegionDistance[ℛ];
df[{1,1}]

A 2D region:

ℛ=MeshRegion[{{0,0},{1,0},{0,1}},Polygon[{1,2,3}]];

df=RegionDistance[ℛ];
{Plot3D[df[{x,y}],{x,-1,2},{y,-1,2},MeshFunctions->{#3&},Mesh->5],ContourPlot[df[{x,y}],{x,-2,3},{y,-2,3},Contours->{{0.5,Red},{1,Green},{1.5,Blue}}]}

A 0D (RefLink[MeshRegion, paclet:ref/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}]}]]

df=RegionDistance[ℛ];
df[{1,1,1}]

A 1D region:

ℛ=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}]}]]

df=RegionDistance[ℛ];
df[{1,1,1}]

A 2D region:

ℛ=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}]}]]

df=RegionDistance[ℛ];
df[{1,1,1}]

A 3D region:

ℛ=MeshRegion[{{0,0,0},{0,0,1},{0,1,0},{1,0,0}},Tetrahedron[{{1,2,3,4}}]];

df=RegionDistance[ℛ];
ContourPlot3D[df[{x,y,z}],{x,-1,2},{y,-1,2},{z,-1,2},Contours->{0.25,0.5,0.95},BaseStyle->Opacity[0.5],Mesh->None]

OpenersExampleSubsection3(Spacer1)Derived Regions  (3)

The distance to a (RefLink[RegionIntersection, paclet:ref/RegionIntersection]):

ℛ=RegionIntersection[Disk[{0,0},1],Disk[{0,1},1]];

df=RegionDistance[ℛ];
df[{1,1}]

Show[Graphics[{Red,Point[{1,1}]}],RegionPlot[ℛ]]

The distance to a (RefLink[TransformedRegion, paclet:ref/TransformedRegion]):

ℛ=TransformedRegion[Disk[],ScalingTransform[{3,2}]];

df=RegionDistance[ℛ];
df[{4,3}]

Show[DiscretizeRegion[ℛ,{{-3,3},{-2,2}}],Graphics[{Red,Point[{4,3}]}]]

The distance to a (RefLink[RegionBoundary, paclet:ref/RegionBoundary]):

ℛ=RegionBoundary[Disk[]];

df=RegionDistance[ℛ];
df[{1/2,1/2}]

Graphics[{ℛ,Red,Point[{1/2,1/2}]}]

OpenersExampleSection3(Spacer1)Applications  (3)

Create a region that is a distance r from a (RefLink[Triangle, paclet:ref/Triangle]):

df=RegionDistance[Triangle[]];

ℛ[r_]=ImplicitRegion[df[{x,y}]<=r,{x,y}]

Visualize the region and triangle:

Region[ℛ[2/3],Epilog->Triangle[]]

Create a region that is a distance r from the given points:

df=RegionDistance[Point[{{0,0,0},{1,1,1}}]];

ℛ[r_]=ImplicitRegion[df[{x,y,z}]<=r,{x,y,z}]

BoundaryDiscretizeRegion[ℛ[1]]

Create a region that is a distance r from a (RefLink[Cuboid, paclet:ref/Cuboid]):

df=RegionDistance[Cuboid[]];

ℛ[r_]=ImplicitRegion[df[{x,y,z}]<=r,{x,y,z}]

RegionPlot3D[ℛ[2]]

OpenersExampleSection4(Spacer1)Properties & Relations  (1)

(RefLink[RegionDistanceFunction, paclet:ref/RegionDistanceFunction]) gives the same value when applied to a point as (RefLink[RegionDistance, paclet:ref/RegionDistance]):

ℛ=MeshRegion[{{0,0},{1,0},{0,1}},Polygon[{1,2,3}]];
df=RegionDistance[ℛ];
pts=RandomReal[1,{10,2}];

df[pts]==(RegionDistance[ℛ,#]&/@pts)

({{DocumentationFooterIconsRelatedFunction.png, ({{See Also}, {(RefLink[RegionDistance, paclet:ref/RegionDistance])  ▪  (RefLink[SignedRegionDistance, paclet:ref/SignedRegionDistance])  ▪  (RefLink[RegionNearestFunction, paclet:ref/RegionNearestFunction])  ▪  (RefLink[RegionMember, paclet:ref/RegionMember])  ▪  (RefLink[RegionNearest, paclet:ref/RegionNearest])}})}})

({{DocumentationFooterIconsRelatedGuide.png, ({{Related Guides}, {▪(RefLinkPlain[Region Properties and Measures, paclet:guide/GeometricPropertiesAndMeasures])}})}})