fkampas
(Frank Kampas)
April 17, 2023, 11:27pm
1
distPolygons[poly1_, poly2_] := {Sqrt[(x1 - x2)^2 + (y1 - y2)^2] /. #, #} & @
QuadraticOptimization[(x1 - x2)^2 + (y1 - y2)^2, {Element[{x1, y1},
poly1], Element[{x2, y2}, poly2]}, {x1, y1, x2, y2}]
p1 = RegularPolygon[{0, 0}, {1, \[Pi]/7}, 3]
p2 = RegularPolygon[{2, 3}, {1, -(\[Pi]/5)}, 4]
sln = distPolygons[p1, p2]
{1.82996, {x1 -> 0.900969, y1 -> 0.433884, x2 -> 1.41221,
y2 -> 2.19098}}
1 Like
Nice! Here is a more lazy way to do it:
d12 = NMinimize[RegionDistance[p2, {x, y}], {x, y} \[Element] p1];
d21 = NMinimize[RegionDistance[p1, {x, y}], {x, y} \[Element] p2];
Graphics[{p1, p2, Red, Line[{{x, y} /. d12[[2]], {x, y} /. d21[[2]]}]}]
2 Likes
fkampas
(Frank Kampas)
April 18, 2023, 12:30pm
3
Henrik,
Your method is much slower than mine. Since the objective function is quadratic and the constraints for a point to be in a polygon are linear, QuadraticOptimization is the best approach.
Frank
Frank,
Your method is much slower than mine.
yes. of course! Being lazy can be costly. Regards – Henrik
2 Likes