Setting step-size with Locator and Manipulate in Mathematica
Given this Mathematica code,
Manipulate[Graphics[Line[{{0, 0}, p}], PlotRange -> 2], {{p, {1, 1}}, Locator}]
How do I set the step distance 开发者_JAVA技巧on the locator? And if possible, constrain them?
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}]
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.
精彩评论