Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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.

share|improve this question
    
Seems like a job for LocatorPane (possibly with DynamicModule). –  István Zachar Oct 17 '13 at 19:44
    
related: mathematica.stackexchange.com/q/29441/5478 –  Kuba Oct 17 '13 at 19:49
    
Thanks for the link..but LocatorPane seem to have the same issue of warping unless I'm missing something. –  george2079 Oct 17 '13 at 20:00
    
"warp" = "bend or twist" is not a good description of the behavior of the first example. –  Michael E2 Oct 17 '13 at 23:52

1 Answer 1

up vote 2 down vote accepted

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?

share|improve this answer
    
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. –  george2079 Oct 17 '13 at 21:01
    
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. –  Kuba Oct 17 '13 at 21:07
    
In the end I think "dont use manipulate" is the real best answer..related link mathematica.stackexchange.com/questions/2464/…. (halirutan's answer) –  george2079 Oct 18 '13 at 15:07

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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