I have a Manipulate to which I am adding and removing Locator points (one of which is different from the others, though that is only context for this question):

lastP = {}; states = {};
setState[p_] := RandomReal[Abs[p[[1]]]];
Manipulate[With[{q = locs[[1]], p = Drop[locs, 1]},
  If[Length[lastP] < Length[p], states = Append[states, setState[p[[-1]]]]];
  If[Length[lastP] > Length[p], states = states[[Flatten[Position[lastP, #] & /@ p]]]];
  lastP = p;
    Text[TableForm[MapThread[List, {p, states}]], {0, 0}], 
    Red, Table[Circle[q, i/30], {i, 3}], 
    Blue, Point[#] & /@ p}, PlotRange -> 2]],
   Button["Clear", {locs = {locs[[1]]}}, ImageSize -> 100, 
    Alignment -> {Center, Baseline}],
   Button["Recalc", states = setState[#] & /@ Drop[locs, 1], 
    ImageSize -> 100, Alignment -> {Center, Baseline}]
   }], {{locs, {{1, 1}}}, Locator, Appearance -> None, 
  LocatorAutoCreate -> 1}]

I need to associate with each of the Locator points in p a random value (according to some specified PDF that is a function of the point's coordinates, here using setState as an example) that is calculated and associated with the point only when it is added or moved, but not when other points are added or moved. I would also like to have access to the association between this value and the point, so that I can adjust the display of each point according to that associated value (e.g. making those with values less than some threshold one color and those greater another).

How can I (ideally without giving up on Manipulate) achieve this "stable" association of calculated values with Locator points? I've played with various approaches for hours, and, with help, I've gotten as far as getting what I want for adding and removing; but I expect there's some simpler approach that I'm missing, and I haven't managed to get moving (i.e., updates to the calculated value associated with a point) working.

  • $\begingroup$ @Kuba: I can't figure out how to get the setState when I move (drag) an existing point. $\endgroup$
    – orome
    Commented Apr 10, 2014 at 21:03
  • $\begingroup$ @Kuba: No idea. That was autocorrect's idea: "get the" should be "recompute". $\endgroup$
    – orome
    Commented Apr 10, 2014 at 21:26
  • $\begingroup$ Ok :P so probably you should correct th sentence "only when it is added or moved, but not when other points are added or moved." $\endgroup$
    – Kuba
    Commented Apr 10, 2014 at 21:43


Your Answer

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

Browse other questions tagged or ask your own question.