2 added 1267 characters in body
source | link

You're correct in that MMA provides a simpler way to find the intersections:

intersections = Solve[{x, y} \[Element] region && {x, y} \[Element] line, {x, y}]

{{x -> 1, y -> 0}, {x -> 2, y -> 0}, {x -> 3, y -> 0}}

Dividing up youyour line is then something of another problem in itself as many factor come into play: Is your line one single straight line? Does it intersect lines in your region more than once? etc etc, So the approach you take will differ depending on what constraints you have.

The following should be a fairly general solution based on parameterising the line and finding the values of the parameter where the intersects occur.

Parameterise[line_] := Module[{linePts, nPts},
  linePts = line[[1]];
  nPts = Length[linePts];

  Interpolation[Transpose[{Rescale@Range[nPts], linePts}], InterpolationOrder -> 1]
  ]

pLine = Parameterise[line];

tab = Table[{u, pLine[u]}, {u, 0, 1, 1/100.}];

intersects = Nearest[tab[[;; , 2]] -> tab[[;; , 1]], #] & /@ intersections[[;; , ;; , 2]];

p = Partition[Join[{0}, Sort[Flatten[intersects]], {1}], 2, 1]

{{0, 0.25}, {0.25, 0.5}, {0.5, 0.75}, {0.75, 1}}

Show[
 DiscretizeRegion[region],
 Table[
  ParametricPlot[pLine[u], {u, p[[i, 1]], p[[i, 2]]}, 
   PlotStyle -> ColorData[97][i]],
  {i, 1, Length@p}
  ]
 ]

enter image description here

To show its versatility:

line = Line@Transpose[{RandomReal[{0, 4}, {4}], RandomReal[{-1, 1}, {4}]}]

Line[{{2.17744, -0.0514017}, {3.69122, -0.940829}, {0.786582, -0.670897}, {1.24424, -0.326177}}]

enter image description here

If I could find a way to invert the pLines function I could do away with the inelegant tab and Nearest components and directly find u when f[u] = {x,y}. Any pointers?

You're correct in that MMA provides a simpler way to find the intersections:

intersections = Solve[{x, y} \[Element] region && {x, y} \[Element] line, {x, y}]

{{x -> 1, y -> 0}, {x -> 2, y -> 0}, {x -> 3, y -> 0}}

Dividing up you line is then something of another problem in itself as many factor come into play: Is your line one single straight line? Does it intersect lines in your region more than once? etc etc, So the approach you take will differ depending on what constraints you have.

You're correct in that MMA provides a simpler way to find the intersections:

intersections = Solve[{x, y} \[Element] region && {x, y} \[Element] line, {x, y}]

{{x -> 1, y -> 0}, {x -> 2, y -> 0}, {x -> 3, y -> 0}}

Dividing up your line is then something of another problem in itself as many factor come into play: Is your line one single straight line? Does it intersect lines in your region more than once? etc etc, So the approach you take will differ depending on what constraints you have.

The following should be a fairly general solution based on parameterising the line and finding the values of the parameter where the intersects occur.

Parameterise[line_] := Module[{linePts, nPts},
  linePts = line[[1]];
  nPts = Length[linePts];

  Interpolation[Transpose[{Rescale@Range[nPts], linePts}], InterpolationOrder -> 1]
  ]

pLine = Parameterise[line];

tab = Table[{u, pLine[u]}, {u, 0, 1, 1/100.}];

intersects = Nearest[tab[[;; , 2]] -> tab[[;; , 1]], #] & /@ intersections[[;; , ;; , 2]];

p = Partition[Join[{0}, Sort[Flatten[intersects]], {1}], 2, 1]

{{0, 0.25}, {0.25, 0.5}, {0.5, 0.75}, {0.75, 1}}

Show[
 DiscretizeRegion[region],
 Table[
  ParametricPlot[pLine[u], {u, p[[i, 1]], p[[i, 2]]}, 
   PlotStyle -> ColorData[97][i]],
  {i, 1, Length@p}
  ]
 ]

enter image description here

To show its versatility:

line = Line@Transpose[{RandomReal[{0, 4}, {4}], RandomReal[{-1, 1}, {4}]}]

Line[{{2.17744, -0.0514017}, {3.69122, -0.940829}, {0.786582, -0.670897}, {1.24424, -0.326177}}]

enter image description here

If I could find a way to invert the pLines function I could do away with the inelegant tab and Nearest components and directly find u when f[u] = {x,y}. Any pointers?

1
source | link

You're correct in that MMA provides a simpler way to find the intersections:

intersections = Solve[{x, y} \[Element] region && {x, y} \[Element] line, {x, y}]

{{x -> 1, y -> 0}, {x -> 2, y -> 0}, {x -> 3, y -> 0}}

Dividing up you line is then something of another problem in itself as many factor come into play: Is your line one single straight line? Does it intersect lines in your region more than once? etc etc, So the approach you take will differ depending on what constraints you have.