Message Boards Message Boards

1
|
1896 Views
|
3 Replies
|
5 Total Likes
View groups...
Share
Share this post:

Distance between polygons

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}}

enter image description here

POSTED BY: Frank Kampas
3 Replies

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

POSTED BY: Frank Kampas

Frank,

Your method is much slower than mine.

yes. of course! Being lazy can be costly. Regards -- Henrik

POSTED BY: Henrik Schachner

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]]}]}]
POSTED BY: Henrik Schachner
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract