1
$\begingroup$

Making an app for conic sections I need that when point ptA is moved, point ptB shifts the same distance and in the same direction of ptA (i.e. a translation). This is the (simplified) code.

enter image description here

Manipulate[ LocatorPane[Dynamic[{ptA, ptB}, 
                        (ptA=#[[1]]; ptB=#[[2]];
                          (* If ptA is moved*)
                          If[ptA!=ptA0, ptB= ptA-ptA0 + ptB; ptA0=ptA]
                         )&], 
Graphics[{ }, Axes->True, PlotRange-> 4]],
Initialization:>{  ptA0={0,0}; ptA={0,0}; ptB={1,0} },
]

On slow machines, when dragging ptA, point ptB doesn't update sufficiently fast. I have 2 questions: If I use Event Handler, does the performance improve? And how can I implement it?

$\endgroup$

closed as unclear what you're asking by m_goldberg, Feyre, corey979, Young, MarcoB Feb 5 '17 at 23:57

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    $\begingroup$ The posted code has severe syntax errors. As it stands, I do not think the intent of the broken code is clear enough to for anyone to work with it. $\endgroup$ – m_goldberg Feb 5 '17 at 18:14
  • $\begingroup$ I lost interest in this question because I guess the problem is actually a hardware problem. By the way, the question is very clear and nobody sees any syntax error. $\endgroup$ – wmora2 Feb 6 '17 at 22:17
  • $\begingroup$ I've improved the performance code by using CurrentValue["CurrentLocatorPaneThumb"] to know which locator is moving, that's all. $\endgroup$ – wmora2 Jan 31 at 1:57
0
$\begingroup$

Does this do what you want? I cannot really test if it is more smooth on your slow system since the original code you provided already ran pretty well on my laptop, but I think this should be a bit more optimized:

DynamicModule[{ptA = {0, 0}, ptB = {1, 0}},
  Graphics[{
    Locator[
      Dynamic[
        ptA,
        (
         ptB = ptB +  # - ptA;
         ptA = #
        ) &
      ]
    ],
    Locator[Dynamic[ptB]]},
    Axes -> True, PlotRange -> 4
  ]
]

edit

Here's a toy implementation of a locatorpane using EventHandler:

DynamicModule[{ptA = {0, 0}, ptB = {1, 1}},
  EventHandler[
    Graphics[{Point[{Dynamic[ptA], Dynamic[ptB]}]}, 
      PlotRange -> {{-3, 3}, {-3, 3}}],
    {
      "MouseClicked" :> If[
        {ptA} === Nearest[{ptA, ptB}, MousePosition["Graphics"]],
        ptA = MousePosition["Graphics"],
        ptB = MousePosition["Graphics"]
      ],
      "MouseDragged" :> If[
        {ptA} === Nearest[{ptA, ptB}, MousePosition["Graphics"]],
        ptA = MousePosition["Graphics"],
        ptB = MousePosition["Graphics"]
      ]
    }
  ]
]
$\endgroup$
  • $\begingroup$ Thanks, but if I use Locator in Graphics, the performance doesn't improve $\endgroup$ – wmora2 Nov 2 '16 at 17:11
  • $\begingroup$ Ok, good to know. To be fair, I don't expect you'll get much of an efficiency boost from trying other methods like EventHandlers. The only other optimization I can come up with is to keep ptB fixed while dragging ptA and then only updating ptB when you release the locator: DynamicModule[{ptA = {0, 0}, ptB = {1, 0}, ptA0}, Graphics[{Locator[ Dynamic[ptA, {(ptA0 = ptA = #) &, (ptA = #) &, (ptB = ptB + # - ptA0; ptA = #) &}]], Locator[Dynamic[ptB]]}, Axes -> True, PlotRange -> 4]] $\endgroup$ – Sjoerd Smit Nov 3 '16 at 9:47
  • $\begingroup$ it seems weird, but if we use Locator Pane we get "better performance" than Locator. That was the reason that I thought that eventHandler could be better. Of course, in a regular fast machines, there is no problems. Anyway, in the few "slow machines" we have, only we have to take care and dragging not so fast. Thanks for your time. $\endgroup$ – wmora2 Nov 3 '16 at 19:40
  • $\begingroup$ That's strange; I wouldn't have expected LocatorPane to be faster. If you're still interested, I rolled a toy example of a custom locatorpane using EventHandler and I will add it to my answer. Maybe it's of use to you. $\endgroup$ – Sjoerd Smit Nov 7 '16 at 14:48
  • $\begingroup$ Fine fine!! I would like to see it. Ah ok, I already saw it $\endgroup$ – wmora2 Nov 8 '16 at 20:59
0
$\begingroup$

Long comment: The OP's code works fine on my machine, but maybe localizing the variables would help:

Manipulate[LocatorPane[Dynamic[{ptA, ptB}, (ptA = #[[1]]; ptB = #[[2]];
     (*If ptA is moved*)
     If[ptA != ptA0, ptB = ptA - ptA0 + ptB; ptA0 = ptA]) &], 
  Graphics[{}, Axes -> True, PlotRange -> 4]],
 {ptA, None}, {ptB, None}, {ptA0, None},
 Initialization :> {ptA0 = {0, 0}; ptA = {0, 0}; ptB = {1, 0}}]

(This might improve speed by reducing Front End <-> Kernel communication; but my LinkSnooper is broken, and I haven't had time to track down the problem.)

$\endgroup$

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