2
$\begingroup$

What I am trying to achieve can be described using following code:

vector = {{0, 0}, {1, 0}};
Graphics[Circle[#, 1] & /@ vector]

In the above, a pure function is applied to Map given vector to construct a set of Circle objects. Same type of logic is applied below, however it does not compile and throws an error:

Coordinate {{0, 0}, {1, 0}} should be a pair of numbers, or a Scaled or Offset form

Code:

DynamicModule[
 {vector, range, radius},
 (*Parameters*)
 radius = 5;
 range = {{0, 100}, {0, 100}};
 vector = {{0, 0}, {1, 0}};

 EventHandler[
  Show[
   Graphics[{
     (*Composition*)
     Line[Dynamic[vector]],
     Circle[#, radius] & /@ Dynamic[vector] (*Error*)
     }],
   (*Options*)
   PlotRange -> range, Axes -> True],
  {
   (*Events*)
   "MouseClicked" :> (AppendTo[vector, MousePosition["Graphics"]])
   }
  ]
 ]

Why does MMA generates the error stated above and how can it be bypassed? Essentially, I want to have a Circle around ea. Point which forms part of the Line.

$\endgroup$
  • $\begingroup$ Dynamic[vector] is incorrect usage. Best way to always think about Dynamic is that it dynamically updates pixels on the screen. When you consider that definition it should be apparent why Dynamic[vector] is incorrect. The next problem that arises from that usage is that the syntax for Circle becomes incorrect which is why you see the pink boxes. $\endgroup$ – Mike Honeychurch Oct 31 '15 at 4:40
4
$\begingroup$

When mapping a vector onto Circle it needs to be just that, a vector with Head List. Putting Dynamic outside of Graphics will allow you to see the circles as they update. Alternatively wrap it around the mapping onto Circle. Additionally your use of Show is redundant.

DynamicModule[{vector, range, radius},
radius = 5;range = {{0, 100}, {0, 100}};vector = {{0, 0}, {1, 0}};

EventHandler[
  Graphics[{Line[Dynamic[vector]], 
    Dynamic[Circle[#, radius] & /@ vector]},
   PlotRange -> range, Axes -> True],
{"MouseClicked" :> (AppendTo[vector, MousePosition["Graphics"]])}]]

enter image description here

$\endgroup$
  • $\begingroup$ You say to put Dynamic outside of Graphics. That is not consistent with your example. Typo? $\endgroup$ – Jack LaVigne Oct 31 '15 at 23:09
  • $\begingroup$ @JackLaVigne you can have it outside of Graphics or wrapped around the mapping onto Circle. $\endgroup$ – Mike Honeychurch Nov 1 '15 at 2:33
4
$\begingroup$

You need just a single Dynamic in front of Show. Because, as stated in the documentation,

Dynamic does its work entirely in the front end, you cannot use it inside functions that need to access the value of an expression in order to do their work.

Prepend Show with Dynamic, will update the graphics output whenever there is a change in the values of the variables used inside:

DynamicModule[
 {vector, range, radius},
 (*Parameters*)
 radius = 5;
 range = {{0, 100}, {0, 100}};
 vector = {{0, 0}, {1, 0}};

 EventHandler[
    Dynamic@Show[
       Graphics[{
          (*Composition*)
          Line[vector],
          Circle[#, radius] & /@ vector
       }]
      (*Options*)
      ,PlotRange -> range
      ,Axes -> True
     ]
    ,(*Events*)
    {"MouseClicked" :> (AppendTo[vector, MousePosition["Graphics"]])}
 ]
]

enter image description here

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