I've got a bunch of lines (ideally in a single Region
) as well as a single new line, e.g.:
region = RegionUnion @@ Line /@ Table[{{x, -1}, {x, 1}}, {x, 3}]
line = Line @ {{0, 0}, {4, 0}}
I want to find all the intersections between region
and line
and want to use those to partition line
into a bunch of sub-lines that don't cross any of the intersections. That is, for the above example I want the lines:
Line /@ { {{0,0}, {1,0}},
{{1,0}, {2,0}},
{{2,0}, {3,0}},
{{3,0}, {4,0}} }
I also need these lines in order (having the order reversed is fine, I just want to be able to find the first and last segment easily).
Of course I could implement this myself by solving a bunch of simple equations, but I was hoping Mathematica's myriad of built-ins allow for a simpler/more idiomatic solution (the issue with solving this based on line intersection is that I have to test for each line in region
separately, whereas the Region*
functions can treat the entire region as a single entity). I tried working with RegionDifference[line, region]
, but Mathematica isn't able to express the resulting segments as new Line
objects (presumably, because RegionDifference
results in open sets and Line
represents a closed set, which I'd be fine with).
Is there any decent way to solve this with a handful of built-ins based on regions, or will I have to roll my own solution solving line equations?