2
$\begingroup$

Here is an example from Mathematica's help:

Manipulate[Graphics[Polygon[pt], PlotRange -> 2], 
{{pt, {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {1, -1}}}, Locator}]

When I execute this, I can drag any Locator, and the Polygon changes.

Is there a way to access which element of pt is being dragged? So I could have a line in the code that does something different while different points are being dragged? Something like:

Manipulate[doSomethingIfPt2IsBeingDragged;Graphics[Polygon[pt], PlotRange -> 2], 
{{pt, {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {1, -1}}}, Locator}]
$\endgroup$
1
$\begingroup$

Generally you access it using the construct pt[[pos]], where "pos" specifies the position of the element in the list of the locator points. In the case at hand it is the list {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {1, -1}}. Its second element, {1,0}, for example is given by pt[[2]], while the first subelement of the second element (i.e. 1) - by pt[[2,1]]. Try the following as an example. It draws a parametric plot changing as soon as you pull the second and/or the fifth locator:

 Manipulate[
 Column[{Graphics[Polygon[pt], PlotRange -> 2],
   ParametricPlot[{Cos[pt[[2, 1]]*t], Sin[pt[[5, 2]]*t]}, {t, 0, 
     2 \[Pi]}]
   }], {{pt, {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {1, -1}}}, Locator}]

enter image description here

Have fun!

$\endgroup$
0
$\begingroup$

This change the color of the polygon (only) while you're dragging it:

Manipulate[
 Dynamic[
  Graphics[{Extract[{Black, Red, Green, Blue}, 
                     Last@Position[col, Except[0.], {1}] + 1], Polygon[pt]}, 
           PlotRange -> 2],
  (col = Total /@ (ptsave - pt); ptsave = pt)],
 {{pt, {{0, 0}, {1, 0}, {1, 1}}}, Locator},
 Initialization :> (ptsave = pt)]
$\endgroup$
  • $\begingroup$ Thanks. I've been doing something like that. I was just wondering if there was a cleaner (built-in) way to access which point was being dragged. $\endgroup$ – rhomboidRhipper Sep 30 '14 at 19:16

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.