Re: Tracking point on a plot
- To: mathgroup at smc.vnet.net
- Subject: [mg76986] Re: Tracking point on a plot
- From: David Bailey <dave at Remove_Thisdbailey.co.uk>
- Date: Thu, 31 May 2007 03:24:33 -0400 (EDT)
- References: <f3jgmj$d8j$1@smc.vnet.net>
Murray Eisenberg wrote:
> I want a locator on the plot of, say, Sin[x], that will move only
> along the graph of the function. How should this be done?
>
> The closest I've come so far is the following, where the x-coordinate
> is being read from wherever the mouse is, but the corresponding
> {x,Sin[x]} point on the curve is where the locator appears:
>
> DynamicModule[{p=0},
> LocatorPane[{Dynamic[p],Dynamic[Sin[p]]},
> Plot[Sin[x],{x,0,10}]
> ]
> ]
>
> (Also, onece the expression is evaluated, each time I move the mouse I
> get a Set::write message about Tag Sin in Sin[...] being protected.)
>
I think this is where the second argument to Dynamic comes in useful:
SetAttributes[ff,HoldAll];
ff[val_,expr_]:=(expr={val[[1]],Sin[val[[1]]]});
DynamicModule[{p={0,0}},LocatorPane[Dynamic[p,ff],Plot[Sin[x],{x,0,10}]]]
For neatness, I expect the function ff could be buried inside the
DynamicModule, but you get the idea.
The above code just uses the X coordinate of the dragged position, but
you could compute the nearest point on the curve if you preferred.
David Bailey
http://www.dbaileyconsultancy.co.uk