Consider a Polygon
with lines drawn between two of its vertices,
pgon = Get[
"https://gist.githubusercontent.com/jasondbiggs/\
59cb7d4aa802bde68c9f6a5203ef698f/raw/\
8b3768422fdcc5c28d03cd1d7a5361c0a1d2d15a/gistfile1.txt"][[2]];
pts = pgon[[1]];
pair1 = {2, 4};
pair2 = {2, 7};
Graphics[{Red,
pgon, {Blue, Line[pts[[#]]], Green, PointSize[Large],
Point[pts[[#]]]} & /@ {pair1, pair2}}]
Clearly one of the lines lies entirely within the polygon, while the other touches the polygon at its endpoints. We can ask for the intersections between these regions
intersections =
RegionIntersection[Line[pts[[#]]], pgon] & /@ {pair1, pair2}
(* {RegionIntersection[
Line[{{7.52229, 9.31563}, {7.93484, 4.23025}}],
Polygon[{{9.23481, 8.12637}, {7.52229, 9.31563}, {8.39443,
4.79338}, {7.93484, 4.23025}, {6.00149, 1.75529}, {7.75656,
2.24465}, {9.438, 3.48553}}]],
RegionIntersection[Line[{{7.52229, 9.31563}, {9.438, 3.48553}}],
Polygon[{{9.23481, 8.12637}, {7.52229, 9.31563}, {8.39443,
4.79338}, {7.93484, 4.23025}, {6.00149, 1.75529}, {7.75656,
2.24465}, {9.438, 3.48553}}]]} *)
Ideally, the system would return either Line
or Point
objects, but it doesn't. We could then ask for the RegionMeasure
of these intersections, to determine whether either of the lines is contained by the polygons,
RegionMeasure /@ intersections
(* {2., 6.13677} *)
The first result is the number of 0-dimensional points at which an intersection occurs, and the second result is the length of the line.
This seems inconsistent. In trying to define a function to determine whether the line lies within the polygon, I would like to get a zero for the case where the line has zero length inside.
I get no help from ArcLength
, RegionDimension
, or RegionEmbeddingDimension
ArcLength /@ intersections
RegionDimension /@ intersections
RegionEmbeddingDimension /@ intersections
(* {2., 6.13677} *)
(* {1, 1} *)
(* {2, 2} *)
Is this expected behavior?
RegionMeasure
it is expected, it is mentioned under "possible issues" in the documentation. $\endgroup$ArcLength
of theIntersection
was the same as the length of the line, then it is in the polygon. It still seems like a non-foolproof method, as there can be cases where the line has a length of exactly 2. $\endgroup$ArcLength
andRegionDimension
are just wrong in this case. I had seen this method but it fails for the case above. I could rephrase the question to be aboutRegionDimension
$\endgroup$