5
$\begingroup$

I am writing a Manipulate expression with two locators. The locators are displayed in a rectangular region given by {{-6, 6}, {-6, 6}}. I would like to restrict the locators to stay in the region given by {{-6, 6}, {0, 5}}, but I cannot figure out how to do it. Also, I am unable to change the size of the locators. I would like to make them smaller, more in line with the grids, but when I give the option ImageSize -> Tiny, the code does not work. Here is a simplified version of my code.

I was wondering if someone could help me with this?

hLines = Graphics[{Gray, 
    Line @@ {Table[{{-6.1, y}, {6, y}}, {y, 0.01, 4.9, 0.25}]}}];
vLines = Graphics[{Gray, 
    Line @@ {Table[{{x, 0.01}, {x, 4.9}}, {x, -6.1, 6, 0.25}]}}];
Manipulate[
  Show[{hLines, vLines}, 
    PlotRange -> {{-6, 6}, {-6, 6}}, 
    Axes -> True,
    AxesOrigin -> {0, 0}, 
    ImageSize -> 400], 
  {{p, {1, 1}}, Locator, Appearance -> Blue}, 
  {{p2, {.5, .5}}, Locator, Appearance -> Red}]
$\endgroup$
  • $\begingroup$ Have you tried to search around? see: constrain+locator. About the size, you can make Locator Appearance None and display whatever you want in p2 position. $\endgroup$ – Kuba Aug 5 '17 at 12:35
2
$\begingroup$

Regarding restriction of a locator to a rectangular region Manipulate >> Details and Options says:

The form {{$u$, $u_{init}$}, {$x_{min}$, $y_{min}$}, {$x_{max}$, $y_{max}$}, $Locator$} specifies a range for the locators.

To give different styles you can use the Appearance option setting:

l1 = Graphics[{EdgeForm[{AbsoluteThickness[2], Blue}], FaceForm[], 
    Table[Rectangle[{-1, -1} i, i {1, 1}], {i, 2}]}, ImageSize -> 15];

Manipulate[Show[{hLines, vLines}, PlotRange -> {{-6, 6}, {-6, 6}}, Axes -> True,
  AxesOrigin -> {0, 0}, ImageSize -> 400],
  {{p, {1, 1}}, {-6, 0}, {6, 5}, Locator, Appearance -> Style["◆", Red, 14]}, 
  {{p2, {.5, .5}}, {-6, 0}, {6, 5}, Locator, Appearance -> l1}]

enter image description here

$\endgroup$
  • $\begingroup$ Ok, I didn't realize I could add 2D limits on the locators p and p2 and thanks for showing how to create a custom locator. This will really help my slide show look more professional as my locators were using Interpolations and when the locator went outside the region, the display would turn red and extrapolation would be attempted which is not what I wanted. $\endgroup$ – Dominic Aug 5 '17 at 13:08
2
$\begingroup$

You can explicitly use LocatorPane[] in Manipulate[]:

Manipulate[LocatorPane[Dynamic[{p, p2}], 
                       ParametricPlot[{x, y}, {x, -6, 6}, {y, 0, 5}, Axes -> True, 
                                      BoundaryStyle -> GrayLevel[1/2, 1/2],
                                      Frame -> False, Mesh -> {40, 20},
                                      MeshStyle -> GrayLevel[1/2, 1/2], PlotRange -> 6,
                                      PlotStyle -> None],
                       {{-6, 0}, {6, 5}}, 
                       Appearance -> {Style["•", Blue, Large], Style["•", Red, Large]}],
           {{p, {1, 1}}, ControlType -> None}, {{p2, {.5, .5}}, ControlType -> None}]

Manipulate with restricted locators

$\endgroup$
2
$\begingroup$

I don't think you need either LocatorPane or Show. It can be more simply with

hLines = {Gray, Line @@ {Table[{{-6.1, y}, {6, y}}, {y, 0.01, 4.9, 0.25}]}};
vLines = {Gray, Line @@ {Table[{{x, 0.01}, {x, 4.9}}, {x, -6.1, 6, 0.25}]}};

Manipulate[
  Graphics[{hLines, vLines},
    PlotRange -> {{-6, 6}, {-6, 6}},
    Axes -> True, 
    AxesOrigin -> {0, 0},
    ImageSize -> 400],
  {{p1, {1, 1}}, {-6, 0}, {6, 5}, Locator, 
    Appearance -> Graphics[{Blue, Disk[]}, ImageSize -> 7]}, 
  {{p2, {.5, .5}}, {-6, 0}, {6, 5}, Locator, 
    Appearance -> Graphics[{Red, Disk[]}, ImageSize -> 7]}]

plot

$\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.