Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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;
  Graphics[{
    Text[TableForm[MapThread[List, {p, states}]], {0, 0}], 
    Red, Table[Circle[q, i/30], {i, 3}], 
    Blue, Point[#] & /@ p}, PlotRange -> 2]],
 Row[{
   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.

share|improve this question
    
@Kuba: I can't figure out how to get the setState when I move (drag) an existing point. –  raxacoricofallapatorius Apr 10 at 21:03
    
@Kuba: No idea. That was autocorrect's idea: "get the" should be "recompute". –  raxacoricofallapatorius Apr 10 at 21:26
    
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." –  Kuba Apr 10 at 21:43

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.