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$
0

2 Answers 2

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$
1
  • $\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$ Sep 30, 2014 at 19:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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