10
$\begingroup$

This is related to the question i just asked here but not the same.

How could i create a manipulate that lets me move the graph to any position i want by clicking and dragging anywhere. Here is an example of what i want at desmos.com.

enter image description here

$\endgroup$
  • 1
    $\begingroup$ I think @szabolcs has posted code for this previously $\endgroup$ – Mike Honeychurch Dec 27 '15 at 23:41
5
$\begingroup$

A version using Manipulate:

Manipulate[
 Graphics[{}, Axes -> True, AxesOrigin -> {0, 0}, PlotRange -> pr, 
  GridLines -> Range @@@ Round@pr, GridLinesStyle -> LightGray], 
 {{p, {0, 0}}, Locator, 
  TrackingFunction -> {p = MousePosition[{"Graphics", Graphics}, {0, 0}]; &, 
    If[MousePosition["GraphicsScaled"] ∈ Rectangle[], 
       pr += p - MousePosition[{"Graphics", Graphics}, {0, 0}]]; &, None}, 
  Appearance -> None}, 
 {{pr, {{-5, 5}, {-5, 5}}}, None}]

enter image description here

$\endgroup$
11
$\begingroup$

This is a direct copy-paste of @m_godberg's code, with a few minor changes to get rid of the viscosity parameter and allow the graphics to move precisely as much as the mouse was dragged.

With[{span = 10.},
  Framed @ 
    DynamicModule[{mouseXY, xmin, xmax, ymin, ymax},
      {xmin, xmax} = {ymin, ymax} = span {-1., 1.}/2.;
      EventHandler[
        Dynamic @ 
          Plot[span Sin[t]/2., {t, xmin, xmax},
          AspectRatio -> Automatic,
          PlotRange -> {{xmin, xmax}, {ymin, ymax}}, 
          GridLines -> {Floor /@ Range[xmin, xmax], Floor /@ Range[ymin, ymax]}],
        {"MouseDown" :> (mouseXY = MousePosition["Graphics"])},
        {"MouseDragged" :>
          Module[{dx, dy},
            {dx, dy} =  (MousePosition["Graphics"] - mouseXY);           
            xmin -= dx; xmax -= dx;
            ymin -= dy; ymax -= dy]}]]]
$\endgroup$
  • $\begingroup$ Ah, I knew there was something bad about my code needing viscosity and having y increase from top to bottom. I totally forgot about the "Graphics" argument. $\endgroup$ – m_goldberg Dec 28 '15 at 13:47
  • $\begingroup$ @m_goldberg If you want to add this alternative to your answer, I'll delete mine for the sake of keeping the thread clean :-) $\endgroup$ – Szabolcs Dec 28 '15 at 13:52
  • 2
    $\begingroup$ No, I'll just add a pointer to your answer to mine. This answer has been up-voted and should stand. $\endgroup$ – m_goldberg Dec 28 '15 at 13:54
  • 1
    $\begingroup$ @m_goldberg Well, the truth is that I would not have taken the effort to implement this fully if I didn't have your code as a starting point ... and I don't care too much about upvotes. $\endgroup$ – Szabolcs Dec 30 '15 at 12:33
11
$\begingroup$

Again I choose a dynamic module to implement your request. This time I don't use a locator, but take direct control of mouse events.

With[{span = 10., viscosity = .01},
  Framed @ 
    DynamicModule[{mouseXY, xmin, xmax, ymin, ymax},
      {xmin, xmax} = {ymin, ymax} = span {-1., 1.}/2.;
      EventHandler[
        Dynamic @ 
          Plot[span Sin[t]/2., {t, xmin, xmax},
          AspectRatio -> Automatic,
          PlotRange -> {{xmin, xmax}, {ymin, ymax}}, 
          GridLines -> {Floor /@ Range[xmin, xmax], Floor /@ Range[ymin, ymax]}],
        {"MouseDown" :> (mouseXY = MousePosition[])},
        {"MouseDragged" :>
          Module[{dx, dy},
            {dx, dy} = viscosity (MousePosition[] - mouseXY);
            mouseXY = MousePosition[];
            xmin -= dx; xmax -= dx;
            ymin += dy; ymax += dy]}]]]

plot

Update

Anyone reading this answer should also read Szabolcs' answer too. He makes important simplifications to my code by fixing a coordinate system error I made and then clumsily worked-around.

$\endgroup$
  • $\begingroup$ I think mouse events are the way to go. Opens up a whole new can of worms for me. But I enjoy worms. Thank you. $\endgroup$ – Michael McCain Dec 28 '15 at 5:25
1
$\begingroup$
  Manipulate[
      Graphics[ 
  AxesOrigin -> a,
  Frame -> True,
  GridLines -> {b = (Range[-10, 10] /. 0 -> {0, Thick}), b},
  PlotRange -> {{a[[1]] - 3, a[[1]] + 3}, 
                {a[[2]] - 3, a[[2]] + 3}}],
 {{a, {0, 0}}, {-5, -5}, {5, 5}, Locator, Appearance -> " "}]
$\endgroup$
  • $\begingroup$ This is almost does it. However, it has as scaling issue that affects single clicking the mouse. If you single click the mouse anywhere in your graph it shouldn't move it. the graph should only move when you drag one point to another point. $\endgroup$ – Michael McCain Dec 27 '15 at 23:58

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.