This post is related to one of my previous posts. My goal is to keep only the lines that fall on the boundary (2, 8)
or inside the region (3, 5)
, or that intersect with the polygon region (1)
.
Please note that lines such as line 7
should be removed.
The code below works for this purpose and the result is what I want, but I'm concerned about its accuracy. When I check RegionIntersection[polygon, #]
with line 4
, it returns an empty region, which is what I want, but it doesn't seem logical to me, as the line and the polygon intersect at one point. Upon further investigation, I found that the point (-2,3)
is represented as {-1.999996701, 3.000003299}
by the line 4
, which explains why there is no intersection.
Does anyone know of a method to ensure that there are no errors related to floating-point numbers or other issues? Also, in my actual case, I have around 4000 functions, so plotting all of them would be slow.
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[2 x - 1, x >= 1],
ConditionalExpression[5, -4 <= x <= -1.5],
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],
ConditionalExpression[2, -2 <= x <= 2],
ConditionalExpression[1, -2 <= x <= -1]};
Show[{region,
Plot[functions, {x, -5, 5}, PlotLegends -> "Expressions"]}]
polygon = Polygon@points;
lines = Cases[
Plot[functions, {x, -5, 5}], {_Directive, l__Line} :>
Line[{l}[[All, 1]]], All];
f = RegionIntersection[polygon, #] === EmptyRegion[2] &;
f /@ lines
(* {False, False, False, True, False, True, True, False} *)
I didn't use the cvgmt code in my previous post because I was concerned about its accuracy. DiscretizeRegion only samples some points, which can lead to potential inaccuracies.
RegionDimension
of at least 1? $\endgroup$RegionDimension
of dimension 1 seems to be exactly what I meant. $\endgroup$