2
$\begingroup$

Continuing with the code at the end this topic, I am now searching for a way of constraining the movement of the locator so that it doesn't "jump" from one "part" of the graph of f to another.

So, if for example the graph of f has an intersection, as in

f[t_] := {Cos[5 t], Sin[4 t]}

then, I'd like the locator to be constrained for increased values of t, so that it "simply" continues the graph of f without "jumping" t-values.

Now, I thus tried, instead of using fvalues as defined in the earlier link, to use something like this

Table[{t, f[t]}, {t, 0, 2, 0.1}];

and then to sort it accordingly with

SortBy[fff, Function[f0, Norm[f0[[2]] - #]]

but when I insert this into the second argument of the Dynamic of the Locator, the constrained movements don't change...

Any help, as always, very much appreciated!

$\endgroup$
  • 1
    $\begingroup$ I think slider is much more "natural" than a locator for those cases $\endgroup$ – Dr. belisarius Sep 21 '13 at 23:03
5
$\begingroup$

Try this (based on your previous Q&A)

f[t_] := t {Cos[10 t], Sin[10 t]}

v = 3;
ptt = Append[f[#], v #] &[1.0];
plot = ParametricPlot[f[t], {t, 0, 2}];
ptfn = Nearest@Table[Append[f[t], v t], {t, 0, 2, 0.01}];
Show[plot, 
 Graphics[{Locator[
    Dynamic[Most[ptt], (ptt = First@ptfn@Append[#, Last[ptt]]) &]]}]]

I add t as the third element to ptt. Here v is the adjustable parameter which means the strength of the prevention of jumps.

enter image description here

$\endgroup$
3
$\begingroup$

To get the point to track the angle of the mouse, one can approach the problem in a different way by using ArcTan to get the angle and Mod to get the angle in the right range.

This below only lets angle increase (by no more than π). Once it gets to the end, the Locator will be stuck there. Reevaluate the code to reset it.

f[t_] := t {Cos[10 t], Sin[10 t]}
ptt = {0., 0.};
Show[
 ParametricPlot[f[t], {t, 0, 2}],
 Graphics[{
   Locator[
    Dynamic[ptt,
            (ptt = f[Clip[Mod[If[# == {0., 0.}, 0, ArcTan @@ #], 2 π, 10 Norm@ptt - π]/10,
                          {Norm@ptt, 2}]]) &]]
   }]]

This will let the angle increase or decrease (by no more than π):

f[t_] := t {Cos[10 t], Sin[10 t]}
ptt = {0., 0.};
Show[
 ParametricPlot[f[t], {t, 0, 2}],
 Graphics[{
   Locator[
    Dynamic[ptt,
            (ptt = f[Clip[Mod[If[# == {0., 0.}, 0, ArcTan @@ #], 2 π, 10 Norm@ptt - π]/10,
                          {Max[Norm@ptt - π, 0], 2}]]) &]]
  }]]

Alternate method

One can also minimize the distance from the curve to the Locator.

f[t_] := t {Cos[10 t], Sin[10 t]} + t {Cos[53 t], Sin[53 t]}/2;
t0 = 0;
ptt = f[t0];
Show[
 ParametricPlot[f[t], {t, 0, 2}],
 Graphics[{Locator[
   Dynamic[
    ptt,
    (t0 = Clip[t /. Last @ FindMinimum[#.# &@(# - f[t]), {t, t0}, 
                                       AccuracyGoal -> 3, PrecisionGoal -> 3],
               {0, 2}]; 
     ptt = f[t0]) &]
  ]}]]

Screenshot of Locator tracking the curve

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.