Mark,
The problem can also be solved recursively. We define a function with two
arguments. The first argument is the interval on which we want to define
which parts of the list of intervals in the second argument are visible. We
construct the (possibly empty) intersection of the first argument with the
highest interval of the second argument, that part is visible. Then to the
left and to the right of this intersection we find two (possibly empty)
intervals on which we have to determine which part of the rest of the second
argument is visible.
Here is an implementation:
In[70]:=
visibleintervals[intervals_] := Module[{f},
f[{a_, b_}, _] /; a\[GreaterEqual]b := Sequence[];
f[{a_, b_}, n_Integer ] := {n, {a, b}};
f[{a_, b_}, {}] := {0, {a, b}};
f[{a_, b_}, {{c_, d_}, e___}] :=
Sequence[f[{a, Max[a,c]}, {e}],
f[{Max[a,c], Min[b, d]}, Length[{e}]+1], f[{Min[b, d],b}, {e}]];
{f[ {Min[First/@intervals], Max[Last/@intervals]}, Reverse[intervals]]}
]
In[71]:=
visibleintervals[{{0, 100}, {2,3}, {1,20}, {15, 30}}]
Out[71]=
{{1,{0,2}},{3,{1,15}},{4,{15,30}},{1,{3,100}}}
In[72]:=
visibleintervals[ {{0,2}, {4, 10}}]
Out[72]=
{{1,{0,2}},{0,{2,4}},{2,{4,10}}}
Regards,
Fred Simons
Eindhoven University of Technology