How to create 2d trajectory by moving mouse over plot

5

1

When I move with the mouse over a plot with a given plot range I would like to store the coordinates of the corresponding trajectory in a list and plot it.

I have seen that MousePosition is used to read the current mouse position in the notebook, but I do not how to start.

lio

Posted 2018-02-20T13:34:50.877

Reputation: 2 196

Answers

6

The following works:

mymouse := Block[{}, pos = MousePosition["Graphics"]; 
              If[pos =!= None, AppendTo[posList, pos]]]
posList = {};
plot = Plot[Sin[x], {x, 0, 4 Pi}]; (* your plot *)

Dynamic[Show[plot, ListLinePlot[posList, PlotStyle -> Red]]]
Dynamic[mymouse]

enter image description here

(This amazing artwork is copyrighted ©anderstood)

Edit Maybe this is a bit smoother?

mymouse := Block[{}, pos = MousePosition["Graphics"];
  If[pos =!= None, AppendTo[posList, pos];
         posList = DeleteDuplicates@posList;]]
(* If[pos =!= None && Not@MemberQ[postList,pos], AppendTo[posList, pos]; *) 
posList = {};
plot = Plot[Sin[x], {x, 0, 4 Pi}];(*your plot*)Dynamic[
 Show[plot, ListLinePlot[posList, PlotStyle -> Red]]]
Dynamic[mymouse]

And this to avoid the slow AppendTo:

i = 1;
mymouse := Block[{}, pos = MousePosition["Graphics"];
  If[pos =!= None, posList[[i]] = pos; i++;]]
posList = ConstantArray[{0, 0}, 1000];
plot = Plot[Sin[x], {x, 0, 4 Pi}];(*your plot*)Dynamic[
 Show[plot, ListLinePlot[posList, PlotStyle -> Red]]]
Dynamic[mymouse]

anderstood

Posted 2018-02-20T13:34:50.877

Reputation: 12 994

1Thank you, this works good, but somehow my on my computer it is very slow: when I move the mouse I have to wait a while to see the curve. Can you also observe this? Another problem: if the mouse is not moved the code continuously stores the same coordinate. Can the code store only once this coordinates? PS: Nobody will be able exactly to repeat your copyrighted artwork, it is unique ;-) – lio – 2018-02-21T09:31:30.533

3@lio I edited, is it better? I tried on two PCs and is was OK, not perfect but OK. To improve that, you would need someone more knowledgeable than me... AppendTo is not recommended but I do not see how to avoid that. I added a third possibility just to see if its smoother, but it introduces the point {0, 0}. – anderstood – 2018-02-22T00:25:37.297

This is excellent for a project I am currently working on; I did not know about MousePosition! Is there an easy way to make this start recording points on the path after a mouseclick, and end after clicking again? – Steve – 2019-04-03T04:28:23.197

@Steve I don't have MMA readily available to try it, but I would start by reading https://reference.wolfram.com/language/ref/EventHandler.html.

– anderstood – 2019-04-03T14:53:25.487

5

ClearAll[addLines]
addLines = DynamicModule[{lc, t = .01, nps = NewPrimitiveStyle /.
       Options[$FrontEnd, NewPrimitiveStyle]},
 Column[{Row[{Button["add lines", FrontEndExecute[
    FrontEnd`Select2DTool["DrawFreehand", "Persistent" -> True]], Alignment -> Center],
  Panel@ColorSetter[Dynamic[lc, (lc = #; SetOptions[$FrontEndSession,
     NewPrimitiveStyle -> {nps = nps /. Rule[LineColor, _] :> Rule[LineColor, #]}]) &],
     BaselinePosition -> Scaled[1/4]],
  Panel@Labeled[LabeledSlider[Dynamic[t, ((t = #; SetOptions[$FrontEndSession,
     NewPrimitiveStyle -> {nps = nps /. Rule[Thickness, _] :>  Rule[Thickness, #]}])) &], 
    {0, 1/10, 1/100}, ImageMargins -> 0, ContentPadding -> False], "thickness", Left]},
 Spacer[1], Alignment -> Center], #}]] &;

Examples:

addLines[Plot[Sin[x], {x, 0, 4 Pi}, ImageSize -> 500]]

enter image description here

addLines[BarChart[Range[10], ImageSize -> 500]]

enter image description here

See also: tutorial/InteractiveGraphicsPalette

kglr

Posted 2018-02-20T13:34:50.877

Reputation: 302 076

1Great use of "DrawFreehand"! However I think your drawing skills lack originality. It's too much copied from Otto Zitko. – anderstood – 2018-02-23T00:42:26.753

@anderstood, my "drawings skills" :) – kglr – 2018-02-23T00:53:56.037

How to store the coordinates of the corresponding trajectory in a list ? – Mariusz Iwaniuk – 2018-02-23T11:13:22.290

@MariuszIwaniuk, I don't know an easy way to modify this to get the coordinates. I will update if I come up with a solution. – kglr – 2018-02-24T02:34:49.950

@kglr. Ok. Thanks for reply. :) – Mariusz Iwaniuk – 2018-02-24T12:18:54.437

1This is great and very fast, but I need to access the coordinates. It would be great if that would be possible. – lio – 2018-02-26T11:44:19.603