3
$\begingroup$

I want to plot two random points when a user presses a button. I have done some work but nothing happens when the button is pressed. The points remain at their current locations.

My code is as follows:

Manipulate[
 DynamicModule[{pts = {{5, 1}, {3, 2}}},
  {
   Graphics[{
     Purple, PointSize[0.02], Point[Dynamic[pts[[1]]]],
     Black, PointSize[0.02], Point[Dynamic[pts[[2]]]],
     If[visualCheck == True, {Black, Circle[CenterPoint, radius]}, {}
      ]
     },
    Axes -> True, PlotRange -> {{-10, 10}, {-10, 10}}, 
    ImageSize -> Large]
   }
  ](*DynamicModule*),
 
 (*Controllers*)
 Button["Click", pts = RandomSample[Tuples[Range[-10, 10], 2], 2]],
 {{CenterPoint, {1, 1}}, Locator},
 {{radius, 2, Style["Radius of the Circle", 12, Black]}, 1, 8, Slider},
 {{visualCheck, False, Style["Visual Check", 12, Black]}, {True, 
   False}, Checkbox}
 
 ]

*I want to change the Black and Purple points when the button is pressed.

Screenshot:

enter image description here

$\endgroup$
2
  • $\begingroup$ why are using DynamicModule inside Manipulate for?? Manipulate itself is DynamicModule You making things way too complicated. If you want I can show you how to I want to plot two random points when the user press a button but I can not follow your code as is. I have to write new one without dynamicmodule inside manipulate. $\endgroup$
    – Nasser
    Sep 23, 2022 at 10:54
  • $\begingroup$ I'm "new" to mathematica, I did not know that Manipulate itself is a DynamicModule thanks for letting me know. if you can show me how you would do it, it will be great $\endgroup$
    – Tt Tt
    Sep 23, 2022 at 11:03

1 Answer 1

3
$\begingroup$

Here is basic implementation. My advice, always start simple and when it is working, you can add more features.

enter image description here

Manipulate[
 Module[{pt1 = pt[[1]], pt2 = pt[[2]]},
  Grid[{
       {pt},
       {Graphics[{{Red,PointSize[0.05],Point[pt1]}, {Blue,PointSize[0.05], Point[pt2]}},
        PlotRange -> {{-10, 10}, {-10, 10}},
      Axes -> True,
      GridLines -> Automatic, GridLinesStyle -> LightGray]}}
    ]
  ]
 ,
 Grid[{{Button["Click", pt = RandomSample[Tuples[Range[-10, 10], 2], 2]]}}],
 {{pt, {{0, 0}, {0, 0}}}, None},
 TrackedSymbols :> {pt}
 ]
$\endgroup$
1
  • $\begingroup$ Thank you, this helps a lot! $\endgroup$
    – Tt Tt
    Sep 23, 2022 at 11:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.