Graphics`Mesh`FindIntersections[ ]
is an undocumented function for, well, detecting intersections very efficiently. Take a look:
i = Import@"https://i.sstatic.net/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]
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]
Is there something that can be done about this behavior?
(1. + 2 $MachineEpsilon)
already works and(1.-$MachineEpsilon)
too (instead of 1.1). How special is this special case? $\endgroup$InfiniteLine[]
instead ofLine[]
might work( tested with the code of Ali Hashmi-answer) ! $\endgroup$