Show or hide components using Manipulate in Mathematica
How could I set a Controller to show or Hide a Graphic component of a Manipulate ?
Manipulate[Graphics[
{Pink, Disk[{1, 1}, r],
Green, Disk[{2, 2}, r]}],
{{r, 0.5, Style["Radius", Black, 16]}, 0.5, 5, 1,
Appearance -> "Labeled"}]
For example, in the above, How could开发者_开发百科 I set a controller to Display or not the Green Circle ?
- Solution :
Manipulate[Graphics[{
If[thePink,
{Pink, Disk[{1, 1}, r]}],
If[theGreen,
{Green, Disk[{2, 2}, r]}]
}
],
{{r, 0.5, Style["Radius", Black, 16]}, 0.5, 5, 1,
Appearance -> "Labeled"},
{{thePink, True, "Pink"}, {True, False}},
{{theGreen, False, "Green"}, {True, False}}]
You can't really "hide" the green ball.
What is displayed is the result of the Manipulate expression evaluation. Manipulate works like this:
Manipulate[ expression, control_variables ]
When any control variable changes (dynamically), the expression is reevaluated and its results displayed. So, when you move the slider, you are changing a variable value, and hence the expression is being re-evaluated and its output displayed.
To "hide" anything, you then need to change the expression NOT to output the green ball. So, you need to add some control variable (say checkbox) and if set, then change the expression not to show the green ball. Simple logic test will do. For example
Manipulate[
Graphics[{Pink, Disk[{1, 1}, r], Green, If[show, Disk[{2, 2}, r]],
Sequence[]}], {{r, 0.5, "Radius"}, 0.5,
5}, {{show, True, "Show Green Circle?"}, {True, False}}]
EDIT:
WOw, thanks Simon, I was about to paste an example of doing just what you did when I saw your edit. thanks. It almost the same code as yours. Here it is might as well paste it :)
Manipulate[Graphics[
{ Pink,Disk[{1,1},r],
If[on,{Green,Disk[{2,2},r]}]
}] ,
{{r,0.5,"Radius"},0.5,5},
{{on,False,"show green ball"},{True,False}}
]
Perhaps:
Manipulate[
Graphics[{Pink, Disk[{1, 1}, r], Opacity[o],
Green, Disk[{2, 2}, r]}],
{{r, 0.5, "Radius"}, 0.5, 5},
{{o, 0.5, "Opacity"}, 0, 1}]
精彩评论