4
$\begingroup$

I have two lists of points as follow and they're not fixed.

ptlist1 = {{-3, 3}, {-5, 6}, {-5, 0}, {5, 0}, {5, 5}, {1, 3}, {-3, 3}};
ptlist2 = {{5, 0}, {5, 5}, {-5, 5}, {-5, 0}};

Now I want to find the boundary of the intersection of two mesh regions formed by these lists and then plot it using ListLinePlot.

This is what I have so far.

region1 = MeshRegion[#, Polygon[Range@Length@#]] &@ptlist1;
region2 = MeshRegion[#, Polygon[Range@Length@#]] &@ptlist2;
intsec= RegionIntersection[region1, region2];
intpts = MeshCoordinates[intsec] ;
ListLinePlot[intpts, GridLines -> Automatic]

The problem is that MeshCoordinates does not generate points in a way that ListLinePlot uses. The first and last point are not the same so the plot by ListLinePlot is not closed. Also these points may not strictly follow one specific direction such clockwise or counterclockwise so the boundary by ListLinePlot is not same as boundary of intsec.

{intsec, ListLinePlot[intpts, GridLines -> Automatic]}

enter image description here

$\endgroup$

1 Answer 1

6
$\begingroup$

Edit

For another cases such as concave region, although the boundary maybe separate to several parts, but still to form a circular circle.

Clear["Global`*"];
SeedRandom[53];
reg1 = BoundaryDiscretizeRegion[Annulus[{0, 0}, {.8, 1}], 
   MaxCellMeasure -> 80];
pts1 = RandomReal[{-1, 1}, {20, 2}];
reg2 = ConvexHullMesh[pts1];
bd = RegionBoundary[
   RegionIntersection[RegionIntersection[reg1, reg2]]];
bds = ConnectedMeshComponents[bd];
colors = RandomColor@Length@bds;
bdpts = MeshCoordinates /@ bds;
bdlines = MeshCells[#, "Multicells" -> True] & /@ bds;
GraphicsGrid[{{Graphics[{{Opacity[.2], Cyan, reg1}, {Opacity[.3], 
      Orange, reg2}, {bd}}], 
   Graphics[Thread[{colors, bds}]]}, {ListLinePlot[
    Append[#, First@#] & /@ bdpts, AspectRatio -> Automatic, 
    PlotStyle -> colors], 
   Graphics[{EdgeForm[Thick], 
     Thread[{colors, 
       MapThread[BoundaryMeshRegion, {bdpts, bdlines}]}]}]}}]

enter image description here

Edit

Clear[pts1, pts2, bd, pts];
pts1 = RandomReal[{-1, 1}, {50, 2}];
pts2 = RandomReal[{-1.5, .8}, {50, 2}];
reg1 = ConvexHullMesh[pts1];
reg2 = ConvexHullMesh[pts2];
bd = RegionBoundary@RegionIntersection[RegionIntersection[reg1, reg2]];
pts = MeshCoordinates@bd;
Show[reg1, reg2, bd, 
 ListLinePlot[Append[pts, First@pts], 
  PlotStyle -> Directive[Opacity[.3], AbsoluteThickness[6], Red]]]

enter image description here

Original

bd = RegionBoundary[intsec];
pts = MeshCoordinates[bd];
Show[bd, ListLinePlot[Append[pts, First@pts], 
  PlotStyle -> Directive[Opacity[.3], AbsoluteThickness[6], Red]]]

enter image description here

$\endgroup$
2
  • $\begingroup$ Thanks, while it works in this case I don't think Append[pts, First@pts] always works as the points may not arranged in the order of CW or CCW. $\endgroup$
    – hana
    Mar 21, 2022 at 13:29
  • 1
    $\begingroup$ @hana Yes, for concave region,the bd maybe separate to several cyclic rings. SeedRandom[1]; reg1 = BoundaryDiscretizeRegion[Annulus[{0, 0}, {.8, 1}], MaxCellMeasure -> 80]; pts1 = RandomReal[{-1, 1}, {20, 2}]; reg2 = ConvexHullMesh[pts1]; bd = RegionBoundary[ RegionIntersection[RegionIntersection[reg1, reg2]]]; Graphics[{{Opacity[.2], Cyan, reg1}, {Opacity[.3], Orange, reg2}, {Red, bd}}] We can draw it by Graphics instead of ListLinePlot since bd is MeshRegion. $\endgroup$
    – cvgmt
    Mar 21, 2022 at 15:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.