Mathematica 9 is now available
Student Support Forum
-----
Student Support Forum: 'Constraining the movement of a Locator Control ...' topicStudent Support Forum > General > Archives > "Constraining the movement of a Locator Control ..."

< Previous CommentHelp | Reply To Comment | Reply To Topic
Author Comment/Response
Michael
07/31/12 1:04pm

There are a couple of ways. I'd recommend the following:

Manipulate[
Plot[f, {x, 0, 10},
Epilog -> {Locator[{x, f} /. x -> Clip[pt[[1]], {0, 10}]]}],
{{pt, {0, 0}}, Locator, Appearance -> None},
{{f, Cos[x]}, InputField}
]

This one is more like what you asked for, though:

Manipulate[
LocatorPane[
Dynamic[pt, (pt = {x, f} /. x -> Clip[#[[1]], {0, 10}]) &],
Plot[f, {x, 0, 10}]
],
{{pt, {0, 1}}, None},
{{f, Cos[x]}, InputField}
]

Notes:
1. Clip is optional; replace Clip[...] with #[[1]] if you prefer. Here it is just to keep the Locator within the PlotRange.
2. In the first example, the display is a function of the invisible Locator, and just uses the x-coordinate. The invisible locator and the coordinates of the variable pt follow the mouse and are not the same as those of the Locator drawn in Epilog.
3. In the second, the second arg. to Dynamic, which updates the variable pt, constrains the y-coordinate to be on the graph. The Locator is where it appears.
4. When things get complicated, the approach in the first example is often easier to debug. That's why I recommend it.
5. Sometimes you have to take the second approach, but I avoid it if I can.
6. One case when the second approach might be preferable is when there are multiple locators. In such a case, it is sometimes helpful to have the locators appear where they actually are. Example:

Manipulate[
LocatorPane[
Dynamic[pts, (pts = {{x, f} /.
x -> Clip[#[[1, 1]], {0, 10}], {x, g} /.
x -> Clip[#[[2, 1]], {0, 10}]}) &],
Plot[{f, g}, {x, 0, 10}]
],
{{pts, {{0, 1}, {0, 0}}}, None},
{{f, Cos[x]}, InputField}, {{g, Sin[x]}, InputField}
]

URL: ,

Subject (listing for 'Constraining the movement of a Locator Control ...')
Author Date Posted
Constraining the movement of a Locator Control ... Bennie 07/29/12 12:03pm
Re: Constraining the movement of a Locator Cont... Michael 07/31/12 1:04pm
< Previous CommentHelp | Reply To Comment | Reply To Topic