Tell me more ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

On a graphic (in this example a simple plot), I need two points to move when I drag with the mouse, and these points must move to location {1,1} when I click on them.

This is how I got it to work, but I don't think it's a good way.

 DynamicModule[{pt = {{1, 2}, {2, 3}}},
  EventHandler[  LocatorPane[Dynamic[pt], Plot[t, {t, -3, 3}], 
  Appearance -> Graphics[Circle[], ImageSize -> 10]], 
 "MouseClicked" :> (pt = 
 ReplacePart[pt, 
  First@Flatten@
     Sort[MapIndexed[{#2, Norm[MousePosition["Graphics"] - #]} &, 
       pt], #1[[2]] < #2[[2]] &] -> {-1, -1}]), 
 "MouseDragged" :> (pt = 
 ReplacePart[pt, 
  First@Flatten@
     Sort[MapIndexed[{#2, Norm[MousePosition["Graphics"] - #]} &, 
       pt], #1[[2]] < #2[[2]] &] -> MousePosition["Graphics"]])]]

Any suggestions for improvement?

share|improve this question
your code is fine. Perhaps, you could replace the second argument of ReplacePart by somewhat shorter Nearest[pt -> {1, 2}, MousePosition["Graphics"], 1] – kguler Jan 31 at 22:35

Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.