# How to select all curves on or outside a region?

How would you select all curves that are on boundary of the region or outside the boundary of the region(including lines with points on the boundary)?

points = {{4, 5.}, {5., 5.}, {5., 0.}, {-5., 0.}, {-5.,
5.}, {-2., 5.}, {-2, 1.}, {2, 1.}, {2, 5}, {4, 5.}};
region = ListLinePlot[points, PlotRange -> {Automatic, {0, 8}},
GridLines -> Automatic, PlotStyle -> Directive[Red, Dashed]];
functions = {ConditionalExpression[Abs[1 - 2 x], x >= 1 || x <= 0],
ConditionalExpression[1, -1 <= x <= 0],
ConditionalExpression[1 - x, 0 <= x <= 1/2],
ConditionalExpression[5 + x, -2 <= x <= 1],
ConditionalExpression[5 - x, 3 <= x <= 4],
ConditionalExpression[6 + x, -1.5 <= x <= 3]};
Show[{g, Plot[functions, {x, -5, 5}]}]


• is the desired result all solid lines except the magenta line?
– kglr
Commented Mar 23, 2022 at 8:43
• @kglr result is all lines except three lines inside the region on the right. I'm not at the PC now so I cannot indicate it on the image.
– hana
Commented Mar 23, 2022 at 8:50
• @kglr I'm sorry that the first function has two branches but part of one branch is inside the region so it's not picked. So there are 3 functions to be removed with one on the boundary and the other two outside the region.
– hana
Commented Mar 23, 2022 at 9:45

polygon = Polygon@points;

lines = Cases[Plot[functions, {x, -5, 5}],
{_Directive, l__Line} :> Line[{l}[[All, 1]]], All];

f = Or[RegionWithin[RegionBoundary[polygon], #],
RegionIntersection[polygon, #] === EmptyRegion[2]] &;

f /@ lines

 {False, True, False, True, False, True}

selected = Select[f]@lines;

Graphics[{EdgeForm[{Red, Dashed}], White, polygon,
Dashing[{}], Thick, MapIndexed[{ColorData[97] @ #2[[1]], #} &] @ selected}]


• Thanks, this is nice too.
– hana
Commented Mar 23, 2022 at 10:20
• I got error with Multicells -> True as the function only accept two agruments.
– hana
Commented Mar 23, 2022 at 14:25
• The code works without error/warning messages in version 11.3.0 (windows) and 13.0 (wolfram cloud).
– kglr
Commented Mar 23, 2022 at 14:35
• my version is 12.2.0.0 (windows). I'll check it
– hana
Commented Mar 23, 2022 at 14:48
• hana, try the updated version.
– kglr
Commented Mar 23, 2022 at 15:05

Edit

reg = Polygon[points];
curves = DiscretizeRegion /@ (ParametricRegion[{x, #}, {{x, -5,
5}}] & /@ functions);
RegionDisjoint[#, reg] & /@ curves


{False, False, False, True, False, True}

Original

dist = RegionDistance[BoundaryDiscretizeGraphics[region]]
Show[region,
Plot[functions, {x, -5, 5},
RegionFunction -> Function[{x, y}, dist[{x, y}] > 0]]]


• I think I didn't explain it clear enough. I want to select all lines/functions where it locate on the boundary or outside the boundary region.
– hana
Commented Mar 23, 2022 at 8:53
• Thanks, the new one works.
– hana
Commented Mar 23, 2022 at 9:45
• I did think to pick some points on the curves then check if they are inside the region but didn't know DiscretizeRegion like that.
– hana
Commented Mar 23, 2022 at 9:54
• I forgot to add that the second one should be True as it is on the boundary.
– hana
Commented Mar 23, 2022 at 15:28