5

I'm trying to implement a widget that will enable users to graphically determine certain abscissa ranges in a 2D plot.

I've started by coding a prototype, whose output looks like this:

The idea for this interface is that the user will be able to adjust the locations of the shaded region's left and right edges by dragging on either one of the dark-blue triangles.

The aim of this *prototype*, however, is for me to understand how `Dynamic`

and `Locator`

fit into the widget's internal structure.

I give the full code for this at the end of this post, but here I want to focus on the code for the shaded region:

```
xrange = {x, -Pi, Pi};
yrange = {y, -1.1, 1.1};
lefthandle = {-2, 0};
righthandle = {2, 0};
triangle[pts_] :=
Graphics[{Directive[ColorData[1][1], Opacity[0.5]], Triangle[pts]}];
regionplot = RegionPlot[
First[lefthandle] < x < First[righthandle],
Evaluate[xrange], Evaluate[yrange],
BoundaryStyle -> None,
Epilog -> {
Inset[triangle[{{-1, 1}, {0, 0}, {-1, -1}}], lefthandle, {-1.2, 0},
Scaled[0.075]],
Inset[triangle[{{1, 1}, {0, 0}, {1, -1}}], righthandle, {1.2, 0},
Scaled[0.075]]
}
];
```

I've tried a bazillion things involving various combinations of `Locator`

and `Dynamic`

^{1}.

They all failed, of course. This is hardly surprising, since I'm basically guessing, using as my starting point the bits of information that the documentation deigns to dole out.

I'm looking for suggestions on how to turn the (now-inert) triangles into draggable handles whose horizontal position determines the width of the shaded region. Specifically: ** where** should

`Locator`

and `Dynamic`

go, and what options should they take?More generally, I would appreciate pointers to write-ups on `Dynamic`

and/or `Manipulate`

that give out the full story on these constructs; IOW, that are written for those (e.g. programmers) who are used to reading technical specifications. (I've read everything the official *Mathematica* documentation has to offer on these constructs, but it's its usual uninformative "dance of seven veils", so I still have no clue of what they are, or how to program with them without resorting to blind trial-and-error.)

There are a couple of important details of the intended use-case that are not captured in the prototype shown in this question. I mention them here, in case they are material to the answer:

In the actual use-case, for any plot there will generally be not just one but several (non-overlapping) regions with adjustable boundaries.

The widget is meant as an aid to help a human (usually me) manually adjust programmatically-generated region boundaries. At the end of the interaction, the widget should deliver a list of region boundaries, some of which may have been the result of the user's interaction with the widget.

Here's the code for the prototype so far^{2}:

```
Module[
{
x, y,
xrange,
yrange,
lefthandle,
righthandle,
plot,
regionplot,
triangle
},
xrange = {x, -Pi, Pi};
yrange = {y, -1.1, 1.1};
lefthandle = {-2, 0};
righthandle = {2, 0};
triangle[pts_] :=
Graphics[{Directive[ColorData[1][1], Opacity[0.5]], Triangle[pts]}];
regionplot = RegionPlot[
First[lefthandle] < x < First[righthandle],
Evaluate[xrange], Evaluate[yrange],
BoundaryStyle -> None,
Epilog -> {
Inset[triangle[{{-1, 1}, {0, 0}, {-1, -1}}], lefthandle, {-1.2, 0},
Scaled[0.075]],
Inset[triangle[{{1, 1}, {0, 0}, {1, -1}}], righthandle, {1.2, 0},
Scaled[0.075]]
}
];
plot = Plot[Sin[x], Evaluate[xrange], PlotRange -> {All, Automatic}];
Show[{regionplot, plot},
PlotRange -> {Automatic, Drop[yrange, 1]},
AspectRatio -> 1/GoldenRatio
]
]
```

^{1I have expressly avoided Manipulate, since using it would mean introducing one more largely undocumented Mathematica MysteryBox into an already confusing situation. I figure that if I have any hope of understanding what I ultimately implement, it may be a good idea to leave Manipulate out of the problem for now. Of course, if what I want to do is just too difficult without Manipulate, then by all means let's have it.}

^{2I decided to post working code that I more-or-less understand, so as not to bias the answers with stuff that I really don't have a clue about. Therefore, I used Module in the place where I suspect one should use DynamicModule (yet another MysteryBox, from my perspective).}

Thanks! I'm sure I will have questions, but it will take me a little while to digest your answer... – kjo – 2015-06-21T00:54:35.677

I was wrong: I have no questions! – kjo – 2015-06-21T02:38:23.953