3
$\begingroup$

I'm looking to have a Manipulator Locator that moves in discrete steps and requies the user to click on/near the locator position.. consider these two examples:

Manipulate[ 
        Graphics[Line[{{0, 0}, q}], 
        PlotRange -> {{-1, 1}, {-1, 1}}], {{x, 
            0}}, {{q, {0, 0}}, {-1, -1}, {1, 1}, .2, Locator}]

The locator moves in discrete steps, but warps wherever you click.

Alternately this does nearly the same:

p = {0, 0};
Manipulate[ 
     Graphics[{Line[{{0, 0}, p}], Locator[Dynamic[p]]}, 
            PlotRange -> {{-1, 1}, {-1, 1}}], {{x, 0}}]

Here you need to click on the locator to drag, but I have no way to specify the grid snap..

Any way to achieve both effects?

( this hack comes to mind as I'm posting ..

p = {0, 0};
    Manipulate[q = Round[5 p]/5; 
       Graphics[{Line[{{0, 0}, q}], Circle[q, .1], Locator[Dynamic[p]]}, 
          PlotRange -> {{-1, 1}, {-1, 1}}], {{x, 0}}]

not very satisfactory though.. )

Edit: Two working solutions based on Kuba's answer, plus a bit of tinkering:

p = {0, 0}; Manipulate[
       Graphics[{Line[{{0, 0}, p}], 
       Locator[Dynamic[
       p, (p = Round[ MousePosition["Graphics", {0, 0}], .2] ) &]]}, 
          PlotRange -> {{-1, 1}, {-1, 1}}], {{x, 0}}]

..

  Manipulate[
      DynamicModule[{p = {0, 0}}, 
          Deploy@Graphics[{Line[{{0, 0}, p}], 
          Locator[Dynamic[p, (p = Round[ #, .2] ) &]]}, 
          PlotRange -> 1]], {{x, 0}}]

I remain a bit puzzled why this doesnt work:

        p = {0, 0}; Manipulate[
            Graphics[{Line[{{0, 0}, p}], 
            Locator[Dynamic[p, (p = Round[#, .2] ) &]]}, 
            PlotRange -> {{-1, 1}, {-1, 1}}], {{x, 0}}]

(The incremental rounding causes the locator to drift away from the mouse pointer). I should rather say I see why this doesnt work, I dont see why DynamicModule fixes it.

$\endgroup$
4
  • $\begingroup$ Seems like a job for LocatorPane (possibly with DynamicModule). $\endgroup$ Commented Oct 17, 2013 at 19:44
  • $\begingroup$ related: mathematica.stackexchange.com/q/29441/5478 $\endgroup$
    – Kuba
    Commented Oct 17, 2013 at 19:49
  • $\begingroup$ Thanks for the link..but LocatorPane seem to have the same issue of warping unless I'm missing something. $\endgroup$
    – george2079
    Commented Oct 17, 2013 at 20:00
  • $\begingroup$ "warp" = "bend or twist" is not a good description of the behavior of the first example. $\endgroup$
    – Michael E2
    Commented Oct 17, 2013 at 23:52

1 Answer 1

3
$\begingroup$

Is this what you are after?

DynamicModule[{p = {0, 0}},
               Deploy@Graphics[{Line[{{0, 0}, p}], 
                                Locator[Dynamic[p, (p = Round /@ #) &]]}
                               , PlotRange -> 5, Frame -> True]
             ]

I'm not sure what do you mean by warp, dragging is warping too?

$\endgroup$
3
  • $\begingroup$ Warp == In my first example you can click anyplace and the locator jumps to wherever you click. This answer is just about what I need -- for some reason if I do the rounding in Locator[Dynamic[]] inside Manipulate[] the locator doesnt track properly with the mouse cursor. Nesting DynamicModule[] inside Manipulate[] seems to work though. $\endgroup$
    – george2079
    Commented Oct 17, 2013 at 21:01
  • $\begingroup$ I'm glad it works like you need. You can skip Manipulate and work with DynamicModule/Dynamic, I find Manipulate useful only for simple GUIs, in other cases it could be frustrating to achieve the result we are after. $\endgroup$
    – Kuba
    Commented Oct 17, 2013 at 21:07
  • $\begingroup$ In the end I think "dont use manipulate" is the real best answer..related link mathematica.stackexchange.com/questions/2464/…. (halirutan's answer) $\endgroup$
    – george2079
    Commented Oct 18, 2013 at 15:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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