3
$\begingroup$

So I have a set of Polygons that I'm obtaining from some ImageJ measurements, and I'd like to sequentially find the intersection points of systematically placed horizontal lines with these polygons. I'm using the following (simplified) code once the polygons have been rotated and translated to the origin.

Here are the coordinates of an example polygon:

{{-74.0165, 299.034}, {-71.8493, 229.211}, {-37.9741, 
  149.258}, {-33.4983, 86.2663}, {-36.5136, 35.7597}, {0., 
  0.}, {31.0483, 0.0471143}, {60.6361, 28.6926}, {69.8234, 
  87.0672}, {75.6655, 142.474}, {75.3829, 243.864}, {82.2615, 
  295.407}, {57.5737, 348.834}, {13.8516, 380.589}, {-31.6137, 
  372.533}, {-71.0012, 349.541}}

And a snippet of how I'm trying to extract the intersection points:

rods = Table[
  Polygon[wholeRodCoords[[i]]],
  {i,1,Dimensions[wholeRodCoords][[1]]}
];
il = InfiniteLine[{{0,1},{1,1}}];
intercepts = RegionIntersection[rods[[1]],il];

Which results in this output:

BooleanRegion[#1&&#2&,{Polygon[],InfiniteLine[{{0,1},{1,1}}]}]

but no discrete points.

If I try a toy example:

poly = Polygon[{{0,0},{1,4},{2,2},{3,-1}}];
line = InfiniteLine[{{1,2},{2,2}}];
RegionIntersection[poly,line]

I get the following expected output:

Line[{{1/2,2},{2,2}}]

So, my primary questions are these:

  1. Is there a better way to do what I'm trying to do?
  2. If not, why is this behavior occurring? I'm not really able to troubleshoot why these expressions are incompatible.
  3. What is a possible solution to this behavior?
$\endgroup$
2
  • 2
    $\begingroup$ try RegionIntersection[rods[[1]], DiscretizeRegion @ il]? $\endgroup$
    – kglr
    May 24, 2023 at 21:50
  • $\begingroup$ This evaluates! Though, I'm not sure how to get the intersection points from the resulting object. $\endgroup$ May 25, 2023 at 14:08

2 Answers 2

4
$\begingroup$
  • To get the full intersection, one way is DiscretizeRegion the result of RegionIntersection.
Clear[pts, poly, line, reg, inter];
pts = {{-74.0165, 299.034}, {-71.8493, 229.211}, {-37.9741, 
    149.258}, {-33.4983, 86.2663}, {-36.5136, 35.7597}, {0., 
    0.}, {31.0483, 0.0471143}, {60.6361, 28.6926}, {69.8234, 
    87.0672}, {75.6655, 142.474}, {75.3829, 243.864}, {82.2615, 
    295.407}, {57.5737, 348.834}, {13.8516, 380.589}, {-31.6137, 
    372.533}, {-71.0012, 349.541}};
poly = Polygon[pts];
line = InfiniteLine[{{1, 2}, {2, 2}}];
reg = RegionIntersection[poly, line]
inter = DiscretizeRegion@reg;
Graphics[{Brown, poly, Red, line, Green, Thick, inter}]
  • Or calculate the intersection of the boundary of the polygon and the line for this case.
intersection =RegionIntersection[RegionBoundary@poly, line]
Graphics[{Brown, poly, Red, line, Green, Thick, Line@@intersection}]

Point[{{-2.04216, 2.}, {33.0654, 2.}}]

enter image description here

  • Test another line.
line = InfiniteLine[{-25, 10}, {-2, 11}];
intersection = RegionIntersection[RegionBoundary@poly, line]
Graphics[{Brown, poly, Red, line, Black, Thick, Green, 
  Line /@ Partition[intersection[[1]], 2, 2], AbsolutePointSize[5], 
  intersection}]

Point[{{-73.2888, 275.588}, {-59.6, 200.3}, {-34.8253, 64.0391}, {-28.2039, 27.6216}}]

enter image description here

$\endgroup$
1
  • $\begingroup$ Excellent! I was not aware that you could call DiscretizeRegion on the BooleanRegion that doesn't evaluate. RegionBoundary is another extremely useful function. This is exactly what I needed. Thanks! $\endgroup$ May 25, 2023 at 14:13
2
$\begingroup$
$Version

(* "13.2.1 for Mac OS X ARM (64-bit) (January 27, 2023)" *)

Clear["Global`*"]

pts = {{-74.0165, 299.034}, {-71.8493, 229.211}, {-37.9741, 
    149.258}, {-33.4983, 86.2663}, {-36.5136, 35.7597}, {0., 0.}, {31.0483, 
    0.0471143}, {60.6361, 28.6926}, {69.8234, 87.0672}, {75.6655, 
    142.474}, {75.3829, 243.864}, {82.2615, 295.407}, {57.5737, 
    348.834}, {13.8516, 380.589}, {-31.6137, 372.533}, {-71.0012, 349.541}};
poly = Polygon[pts];
line = InfiniteLine[{{0, 1}, {1, 1}}];

Use RegionConvert to convert the BooleanRegion to an ImplicitRegion

reg = RegionConvert[
  RegionIntersection[poly, line], "Implicit"]

enter image description here

Use Reduce to simplify the ImplicitRegion

intercepts = ReplacePart[reg,
  1 -> N[Reduce[reg[[1]] // Rationalize[#, 0] &]]]

enter image description here

Show[Region[Style[poly, LightBlue]],
 Region[Style[line, LightGray, Thick, Dashed]],
 Region[Style[intercepts, Red, Thick]],
 AspectRatio -> 1]

enter image description here

Using the same approach with cvgmt's second example:

line2 = InfiniteLine[{-25, 10}, {-2, 11}];

reg2 = RegionConvert[
   RegionIntersection[poly, line2], "Implicit"];

intercepts2 = ReplacePart[reg2,
  1 -> N[Reduce[reg2[[1]] // Rationalize[#, 0] &]]]

enter image description here

Show[Region[Style[poly, LightBlue]],
 Region[Style[line2, LightGray, Thick, Dashed]],
 Region[Style[intercepts2, Red, Thick]],
 AspectRatio -> 1]

enter image description here

$\endgroup$
1
  • $\begingroup$ This is an interesting approach. I think the implicit region would be more useful in certain scenarios, but I think the approach by cvgmt is more useful in my case. Although, now that I think about it, the Implicit approach is probably more performant? $\endgroup$ May 25, 2023 at 14:15

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.