Mathematica Asked by DavidC on February 6, 2021
This follows up on another question about the sensitivity of Locators in a a LocatorPane.
I would like to be able to enable/disable individual locators in a LocatorPane.
In the simplified version of the applet, pictured below, I would like to be able to disable the locators that set the slope of the red line, while allowing the locators that set the slope of the blue line to remain enabled.
Using individual Locator
s, rather than a LocatorPane
, is not an option. (There are some subtle issues that arise with individual locators. Essentially, multiple Locator
s can behave in a “flaky” fashion in complex applets, in ways that LocatorPane
does not.)
Code below:
Manipulate[m = 15;
LocatorPane[Dynamic[pts],
Dynamic[ Module[{x = pts[[1, 1]], y = pts[[2, 2]], x2 = pts[[3, 1]], y2 = pts[[4, 2]]},
Graphics[{
{Blue, Line[{-m*{x2, y2}, m*{x2, y2}}]},
Line[{{x2, 0}, {x2, y2}}], Line[{{x2, y2}, {0, y2}}],
{Red, Line[{-m*{x, y}, m*{x, y}}]},
Line[{{x, 0}, {x, y}}], Line[{{x, y}, {0, y}}]},
PlotRange -> m, Axes -> True, ImageSize -> {300, 300}]]],
{{{-m, 0}, {m, 0}, {1, 0}},
{{0, -m}, {0, m}, {0, 1}},
{{-m, 0}, {m, 0}, {1, 0}},
{{0, -m}, {0, m}, {0, 1}}},
Appearance -> {Automatic, Automatic, Automatic, Automatic}],
{{pts, {{6, 0}, {0, 9}, {3, 0}, {0, 7}}}, ControlType -> None}]
The visibility of the locators can be individually controlled by toggling the respective locator’s Appearance
between None
and Automatic
. But even when the locator is invisible (i.e. Appearance -> None
) it continues enabled. For example, the red sliders will continue to set the slope of the red line.
A possible solution would be to obtain the Appearance
setting of the red sliders and make the assignment of x
and y
contingent on the Appearance setting.
A simple way to do this is to change the Dynamic
so that it updates only the points you want to be editable. Here is a very simple demonstration:
pts = {{6, 0}, {0, 9}, {3, 0}, {0, 7}};
updatable = Range@Length@pts;
Button["Fixate point 3", (updatable = {1, 2, 4})]
LocatorPane[Dynamic[pts, (pts[[updatable]] = #[[updatable]]) &],
Dynamic@Graphics[Point /@ pts, PlotRange -> {{-10, 10}, {-10, 10}}]]
Answered by jVincent on February 6, 2021
I like @jVincent's approach.
Here is a different approach Overlay
ing LocatorPanes
inside Manipulate
:
locPane[Dynamic[pts_], shape_, color_] :=
LocatorPane[Dynamic[pts],
Dynamic@Module[{x = pts[[1, 1]], y = pts[[2, 2]]},
Graphics[{{color, Line[{-m*{x, y}, m*{x, y}}]},
Line[{{x, 0}, {x, y}}], Line[{{x, y}, {0, y}}]}, PlotRange -> m,
Axes -> True, ImageSize -> {300, 300}]],
{{{-m, 0}, {m, 0}, {1, 0}}, {{0, -m}, {0, m}, {0, 1}}},
Appearance -> {shape, shape}];
(* locator apperance graphics *)
redCircle = Graphics[{Red, Table[Circle[{0, 0}, i], {i, 2}]}, ImageSize -> 10];
blueCircle = Graphics[{Blue, Table[Circle[{0, 0}, i], {i, 2}]}, ImageSize -> 10];
grayDisk = Graphics[{Lighter@Lighter@Gray, Disk[{0, 0}, 2],
Gray, Circle[{0, 0}, 3]}, ImageSize -> 10];
Method 1: Overlay
two instances of locPane
inside a Manipulate
Manipulate[m = 15;
Overlay[{
locPane[Dynamic[pts], Dynamic@If[layer == 1, redCircle, grayDisk], Red],
locPane[Dynamic[pts2], Dynamic@If[layer == 2, blueCircle, grayDisk], Blue]},
All, Dynamic[layer]],
{{pts, {{6, 0}, {0, 9}}}, ControlType -> None},
{{pts2, {{7, 0}, {0, 7}}}, ControlType -> None},
{{layer, 1, "layer"}, {1, 2}}]
to get
Method 2: Overlay
two instances of locPane
inside an EventHandler
and Manipulate
:
Right-mouse-click toggles between two locator panes:
DynamicModule[{layer = 1}, EventHandler[Manipulate[m = 15;
Overlay[
{locPane[Dynamic[pts], Dynamic@If[layer == 1, redCircle, grayDisk], Red],
locPane[Dynamic[pts2], Dynamic@If[layer == 2, blueCircle, grayDisk], Blue]},
All, Dynamic[layer]],
{{pts, {{6, 0}, {0, 9}}}, ControlType -> None},
{{pts2, {{7, 0}, {0, 7}}}, ControlType -> None}],
{{"MouseClicked", 2} :> ((layer = layer /. {1 -> 2, 2 -> 1}))},
PassEventsDown -> True]]
Answered by kglr on February 6, 2021
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP