26

Graphics`Mesh`FindIntersections[ ] is an undocumented function for, well, detecting intersections very efficiently. Take a look:

i = Import@"https://i.stack.imgur.com/PcWcz.png";
perim = ImageValuePositions[Thinning@EdgeDetect@i, 1];
fcp = FindCurvePath[perim];
perimPts = perim[[First@fcp]];
Graphics`Mesh`MeshInit[]; (*not sure if needed*)
myLine = Line@{1.1 {750.5`, 955.5`}, {182.5`, 671.5`}};
pts = Graphics`Mesh`FindIntersections[{myLine, Polygon@perimPts}];
Graphics[{Line@perimPts, Red, myLine, Green, PointSize[Large], 
          Point@pts}, AspectRatio -> Automatic]

Mathematica graphics

But it doesn't work consistently (here I change myLine):

myLine = {Line@{{750.5`, 955.5`}, {182.5`, 671.5`}}, 
          Line@{{622.5`, 1031.5`}, {222.5`, 831.5`}}};
pts = Graphics`Mesh`FindIntersections[{#, Polygon@perimPts}] & /@ myLine;
Graphics[{Line@perimPts, Red, Sequence @@ myLine, Green, 
          PointSize[Large], Point /@ pts}, AspectRatio -> Automatic]

Mathematica graphics

Is there something that can be done about this behavior?

Alexey Popkov
  • 61,809
  • 7
  • 149
  • 368
Dr. belisarius
  • 115,881
  • 13
  • 203
  • 453

2 Answers2

4

(Not an answer, just an illustrated comment)

Let us look closer where the points missing on the plot shown by the OP are located:

Graphics[{Blue, Line@perimPts, PointSize[Large], Point@perimPts, Red, myLine}, 
   AspectRatio -> Automatic, Frame -> True, PlotRange -> #, 
   GridLines -> Automatic] & /@ 
 {{{323, 326}, {881, 884}}, {{624, 626}, {892, 894}}, {{548, 550}, {994, 996}}}

output

Two missing intersection points are located exactly at the nodes of the polyline, while the third point isn't.

Alexey Popkov
  • 61,809
  • 7
  • 149
  • 368
3

I think the problem might be in the way we preprocess the image. Here is a fix:

(* finding the boundary *)
i = Import@"https://i.stack.imgur.com/PcWcz.png";
img = MorphologicalPerimeter@Binarize@FillingTransform@ColorNegate@i;
boundaryx = PixelValuePositions[img, 1];
path = FindShortestTour[boundaryx][[2]];
boundaryx = boundaryx[[path]];



myLine = Line@{1.1 {750.5`, 955.5`}, {182.5`, 671.5`}};
gr = Graphics[{FaceForm[], EdgeForm[Blue], Polygon@boundaryx, Red, myLine}];
pts = Graphics`Mesh`FindIntersections[
Graphics[{FaceForm[], EdgeForm[Blue], Polygon@boundaryx, myLine}]];
Show[gr, Graphics[{Red, PointSize[Large], Darker@Green, Point@pts}]]

enter image description here

myLine = {Line@{{750.5`, 955.5`}, {182.5`, 671.5`}}, 
Line@{{622.5`, 1031.5`}, {222.5`, 831.5`}}};
gr = Graphics[{FaceForm[], EdgeForm[Blue], Polygon@boundaryx, myLine}];
pts = Graphics`Mesh`FindIntersections[{#, Polygon@boundaryx}] & /@ myLine;
Show[gr, Graphics[{Red,Sequence @@ myLine,Darker@Green,
PointSize[Large], Point /@ pts}, AspectRatio -> Automatic]]

enter image description here

Ali Hashmi
  • 8,950
  • 4
  • 22
  • 42
  • It is interesting that with the perimPts of OP Graphics`Mesh`FindIntersections works correctly if we apply Ceiling of Floor to perimPts, but applying Round reveals another bug. – Alexey Popkov Jan 20 '18 at 12:17