# Constrain Locator to specified region

How can I constrain the locator to stay within the region defined by RegionPlot?

When the Locator remains within the region, NDSolve generates a periodic solution. (The boundary of the region represents a homoclinic orbit for the DE. When the Locator is outside the boundary of the region, NDSolve generates an unbounded orbit. Actually, the boundary of the region is the solution to the DE with initial condition $x(0) = -6,\; y(0) = 0$.)

Additionally a strange behavior occurs whenever the left mouse button is pressed: the right end of the region is truncated. Why?

Manipulate[
region = RegionPlot[y^2 < x^2*(1 + x/6), {x, -6, 0}, {y, -2.5, 2.5}];
sol = NDSolve[{x'[t] == y[t], y'[t] == x[t] + x[t]^2/4,
x == p[], y == p[]}, {x, y}, {t, 0, T}];
psol = ParametricPlot[Evaluate[{x[t], y[t]} /. sol], {t, 0, T},
PlotRange -> {{-6, 0}, {-3, 3}}, PlotStyle -> Red ];
Show[{region, psol}], {{p, {-2, 0}}, Locator}, {{T, 5}, 0, 12, 0.1}]


I suspect that Dynamic needs to be introduced here, but I don't know how to implement it successfully.

• Add the option PerformanceGoal -> "Quality" to RegionPlot to override the default settings PerformanceGoal -> ControlActive["Speed", "Quality"] for plots inside Manipulate. – kglr May 1 '16 at 20:44
• Have you searched for related questions? This one for example. – C. E. May 1 '16 at 20:53

## 2 Answers

• How can I constrain the Locator to stay within the region defined by RegionPlot?

You can check if Locator's coordinates fulfill the condition defining your region. It can be done with the second argument of Dynamic if you introduce Locator explicitly. Take a look at line with Locator[Dynamic[p, With[{...

• A strange behavior occurs whenever the left mouse button is pressed: the right end of the region is truncated. Why?

The body of a Manipulate is effectively wrapped with Dynamic. Each time you move something, it will be evaluated. During dynamic evaluation \$PerformanceGoal is set to "Speed" unless you change it. It results in less sampling points and cut corners.

You can change it to "Quality" but here there is no point in evaluating region each time anyway. It is independent from T and p so let's do it outside, once for good.

Edit:

• Your answer is just what I wanted though I need to create a CDF demo. Unfortunately I obtained an error message upon creating a CDF from your answer.

That's because region definition is forgotten as soon as the Kernel is quit, in contrast to Manipulates and DynamicModules variables generated "on fly" like sol and psol here.

You can use SaveDefinitions->True or inject the region with With.

• I also tried to change the appearance of the locator to a disk but was challenged there as well.

For custom Locators it is better to set Appearance->None and display whatever you want in its coordinates.

With[{
region = RegionPlot[y^2 < x^2*(1 + x/6), {x, -6, 0}, {y, -2.5, 2.5}]
},

Manipulate[
sol = NDSolve[
{x'[t] == y[t], y'[t] == x[t] + x[t]^2/4,
x == p[], y == p[]},
{x, y}, {t, 0, T}
];
psol = ParametricPlot[Evaluate[{x[t], y[t]} /. sol], {t, 0, T},
PlotRange -> {{-6, 0}, {-3, 3}}, PlotStyle -> Red
];

Show[{
region, psol,
Graphics[{
Dynamic @ Disk[p, .1],
Locator[Dynamic[p,
With[{x = #[], y = #[]}, If[y^2 < x^2*(1 + x/6), p = #]] &],
Appearance -> None
]
}]
},
AspectRatio -> Automatic
],
{{p, {-2, 0}}, None}, {{T, 5}, 0, 12, 0.1}
]] • Your answer is just what I wanted though I need to create a CDF demo. Unfortunately I obtained an error message upon creating a CDF from your answer. I also tried to change the appearance of the locator to a disk but was challenged there as well. If these comments go beyond the parameters of my original question, I will resubmit as a new question. – Stephen May 2 '16 at 11:29
• @Stephen region will not be found unless you use SaveDefinitions ->True for the Manipulate. You can use Appearance->None on locator and display a disk separately. Will include those things to my answer later. – Kuba May 2 '16 at 11:31
• Your comment was all I needed. All works well now: CDF and the appearance of the locator, I appreciate your explanations in your answer that you posted yesterday. – Stephen May 2 '16 at 12:15

Here's a variation of Kuba's answer that uses RegionNearest[]:

Manipulate[Show[RegionPlot[region],
With[{sol = NDSolveValue[{x''[t] == x[t] + x[t]^2/4,
x == p[], x' == p[]}, x,
{t, 0, T}]},
ParametricPlot[{sol[t], sol'[t]}, {t, 0, T},
PlotRange -> {{-6, 0}, {-3, 3}}, PlotStyle -> Red]],
Graphics[{Dynamic[Disk[p, .1]],
Locator[Dynamic[p, (p = rnf[#]) &], Appearance -> None]}],
AspectRatio -> Automatic],
{{p, {-2, 0}}, None}, {{T, 5}, 0, 12, 1/10},
Initialization :> (region = ImplicitRegion[y^2 < x^2*(1 + x/6),
{{x, -6, 0}, {y, -2.5, 2.5}}];
rnf = RegionNearest[region];)]


which should yield the same interface as his answer.