6
$\begingroup$

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.

enter image description here

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.

$\endgroup$
2
  • $\begingroup$ What is your exact definition of overlap? Is it that region intersection should have RegionDimensionof at least 1? $\endgroup$
    – kirma
    Feb 28, 2023 at 15:09
  • 1
    $\begingroup$ @kirma that is a new concept to me but RegionDimension of dimension 1 seems to be exactly what I meant. $\endgroup$
    – hana
    Feb 28, 2023 at 15:13

1 Answer 1

7
$\begingroup$
(* Rationalise points, convert polygon to an implicit region form. *)
polygon = RegionConvert[Polygon@Rationalize@points, "Implicit"];

(* Convert functions to implicit region form with
   inexact values rationalised. *)
lines = functions /. 
   ConditionalExpression[yval_, dom_] :> 
    ImplicitRegion[Rationalize[y == yval && dom], {x, y}];

(* Find those lines whose intersection with polygon is not empty and
   doesn't consist only of detached point(s). *)
RegionDimension[RegionIntersection[polygon, #]] >= 1 & /@ lines

(* {True, True, True, False, True, False, False, True} *)

There are couple of things of relevance on this code:

  • Avoid inexact numbers. They will cause even more pain than you have observed with region intersections.
  • Apparently there's yet another bug with Polygons when handing regions. Thankfully converting the polygon to implicit form works around it.

EDIT:

If you don't have RegionConvert in your version of Mathematica, you can substitute it in this case with the following:

ClearAll[RegionConvert];

RegionConvert[reg_, "Implicit"] :=
 Module[{x},
  With[
   {vars = Array[x, RegionEmbeddingDimension[reg]]},
   ImplicitRegion[
    Refine[RegionMember[reg, vars],
     Element[Alternatives @@ vars, Reals]],
    vars]]]
$\endgroup$
3
  • $\begingroup$ That looks nice. I'll check it a bit later as I'm using old version now which does not have RegionConvert. $\endgroup$
    – hana
    Feb 28, 2023 at 15:29
  • 1
    $\begingroup$ @hana I added a workaround for missing RegionConvert which I tested on v12.2. $\endgroup$
    – kirma
    Feb 28, 2023 at 15:43
  • 1
    $\begingroup$ Since this appears to be a bug, I was wondering if you know of any possible workarounds? $\endgroup$
    – hana
    Mar 2, 2023 at 17:09

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.