Question

Given this Mathematica code,

Manipulate[Graphics[Line[{{0, 0}, p}], PlotRange -> 2], {{p, {1, 1}}, Locator}]

How do I set the step distance on the locator? And if possible, constrain them?

Was it helpful?

Solution

You could do something like

Manipulate[
 Graphics[Line[{{0, 0}, p}], 
  PlotRange -> 2], {{p, {1, 1}}, {-1, -1}, {1, 1}, {0.4, 0.5}, Locator}]

which would restrict the locator to a rectangular lattice with a horizontal spacing of 0.4 and a vertical spacing of 0.5. The range of the coordinates for the locator is specified by {xmin,ymin} = {-1,-1} and {xmax, ymax} = {1,1}.


If you want more flexibility, e.g. you want to restrict the position of locator to a non-rectangular lattice or to a more general set of coordinates you could do something like

Manipulate[
 With[{tab = RandomReal[{-1, 1}, {40, 2}]}, 
  LocatorPane[Dynamic[p, (p = Nearest[tab, #][[1]]) &], 
   Graphics[{Line[{{0, 0}, Dynamic[p]}], {Red, Point /@ tab}}, PlotRange -> 2]]],
 {{p, {1, 1}}, ControlType -> None}]

OTHER TIPS

The documentation states:

Manipulate[expr, {u, umin, umax, du}]

allows the value of u to vary between umin and umax in steps du.

and

Manipulate[expr, {u, {u1, u2, u3,...}}]

allows u to take on discrete values.

One of these approaches should work for you.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top