- construct two lines to make a closed curve and then use
BoundaryDiscretizeGraphics
to get a region.
Clear[f, g, plot1, plot2, pts1, pts2, reg];
f[x_] = ConditionalExpression[2*x - 2, 2 < x < 6];
g[x_] = ConditionalExpression[2*x + 2, x < 4];
plot1 = Plot[f[x], {x, 0, 10}, PlotStyle -> Red];
plot2 = Plot[g[x], {x, 0, 10}, PlotStyle -> Green];
pts1 = Cases[plot1, Line[pts_] :> pts, Infinity][[1]];
pts2 = Cases[plot2, Line[pts_] :> pts, Infinity][[1]];
reg = Graphics[{Line[pts1], Line[pts2],
Line[{pts1[[-1]], pts2[[-1]]}], Line[{pts1[[1]], pts2[[1]]}]}] //
BoundaryDiscretizeGraphics;
Show[reg, plot1, plot2, Axes -> True]
- test another two functions which does not intersection each other.
Clear[f, g, plot1, plot2, pts1, pts2, reg];
f[x_] = ConditionalExpression[1/x - 2, 2 < x < 6];
g[x_] = ConditionalExpression[1/4 x^2 + 2, x < 4];
plot1 = Plot[f[x], {x, 0, 10}, PlotStyle -> Red];
plot2 = Plot[g[x], {x, 0, 10}, PlotStyle -> Green];
pts1 = Cases[plot1, Line[pts_] :> pts, Infinity][[1]];
pts2 = Cases[plot2, Line[pts_] :> pts, Infinity][[1]];
reg = Graphics[{Line[pts1], Line[pts2],
Line[{pts1[[-1]], pts2[[-1]]}], Line[{pts1[[1]], pts2[[1]]}]}] //
BoundaryDiscretizeGraphics;
Show[reg, plot1, plot2, Axes -> True]
Clear[pts1, pts2, graphics, reg]
SeedRandom[1];
pts1 = SortBy[RandomPoint[Rectangle[{-1, 4}, {10, 5}], 10], First];
pts2 = SortBy[RandomPoint[Rectangle[{-1, 0}, {10, 2}], 10], First];
graphics =
Graphics[{Line[pts1], Line[pts2], Line[{pts1[[-1]], pts2[[-1]]}],
Line[{pts1[[1]], pts2[[1]]}]}];
reg = graphics // BoundaryDiscretizeGraphics;
Show[reg, ListLinePlot[{pts1, pts2}, PlotStyle -> {Red, Green}],
Axes -> True]