RegionNearest gives the point in a region nearest to a point outside the region. The following function finds the points in two regions nearest the other region and the distance between the points. The arguments are the two regions, a starting point in the first region, and a tolerance, since the calculation may be iterative.
In[]:=
min[reg1_,reg2_,reg1StartPoint_List,tolerance_]:=Block[{rn1,rn2,pt1,pt2},rn1=RegionNearest[N@reg1];rn2=RegionNearest[N@reg2];iter[pt_List]:=rn1[rn2[pt]];pt1=FixedPoint[iter,reg1StartPoint,SameTest->(Norm[#2-#1]<tolerance&)];pt2=rn2[pt1];{Norm[pt1-pt2],{pt1,pt2}}]
An Ellipse and a Polygon
An Ellipse and a Polygon
In[]:=
reg1=Disk[{-2,0},{2/3,4/3}];
In[]:=
reg2=RegularPolygon[{2,3},{1,Pi/5},3];
In[]:=
sln=minreg1,reg2,{-2,0},
-6
10
Out[]=
{3.54806,{{-1.62206,1.09837},{1.24838,3.18386}}}
In[]:=
Show[Graphics@{Opacity[0.2],reg1},Graphics@{Opacity[0.2],reg2},Graphics@Line[sln[[2]]],Frame->True,ImageSize->Small]
Out[]=
A Convex Polygon and a Non-Convex Polygon
A Convex Polygon and a Non-Convex Polygon
In[]:=
p1=Polygon[CirclePoints[3]];
In[]:=
p2=Polygon[{{2,-1},{4,-1},{4,1},{2,2},{3,0}}];
In[]:=
sln=min[p1,p2,{0,0},]
-6
10
Out[]=
{1.23931,{{0.866025,-0.5},{2.,-1.}}}
In[]:=
Show[Graphics@{Opacity[0.2],{p1,p2}},Graphics@Line[sln[[2]]],Frame->True,ImageSize->Small]
Out[]=
An Ellipse and a Non-Convex Polygon
An Ellipse and a Non-Convex Polygon
In[]:=
sln=minreg1,p2,{-2,0},
-6
10
Out[]=
{3.41683,{{-1.36869,-0.428448},{2.,-1.}}}
In[]:=
Show[Graphics@{Opacity[0.2],{reg1,p2}},Graphics@Line[sln[[2]]],Frame->True,ImageSize->Small]
Out[]=
CITE THIS NOTEBOOK
CITE THIS NOTEBOOK
Extending RegionNearest to find nearest two points in two regions and their distance
by Frank Kampas
Wolfram Community, STAFF PICKS, October 13, 2024
https://community.wolfram.com/groups/-/m/t/3297016
by Frank Kampas
Wolfram Community, STAFF PICKS, October 13, 2024
https://community.wolfram.com/groups/-/m/t/3297016