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.

This bit of code sets up a list with the first entry already in it -- {{1,1}}

Clear[x, y]
x = 1
y = 1
datalist=List[List[x, y]]

Here is a slider by which I can alter x and y by depressing the mouse.

Slider2D[{x, y}, {{0, 0}, {5, 8}, {1, 2}}]

But when I release the mouse button nothing happens of course, except that {x,y} are at some new value inside the slider.

Suppose I have moved the slider to location {3,6}. When I release the mouse button, I want {3,6} to be appended to datalist so it now becomes {{1,1},{3,6}}

When I am tired of adding data points I want to save datalist

I want to make as few changes/additions to the above code as possible.

Any ideas?

share|improve this question
add comment

3 Answers 3

This is a job for EventHandler:

EventHandler[
   Slider2D[Dynamic[{x, y}], {{0, 0}, {5, 8}, {1, 2}}],
   {"MouseUp" :> AppendTo[datalist, {x, y}]},
   PassEventsDown -> True
]

Mathematica graphics

Follow the data acquisition with a Dynamic:

Dynamic[datalist]

{{1,1},{3,4},{2,2},{2,4}}

PassEventsDown -> True is necessary to prevent EventHandler from eating up the MouseDrag events that are necessary to make the Slider2D move. To let Slider2D update x and y the Dynamic is necessary. This is well documented in the Slider2D documentation.

share|improve this answer
add comment

Alternatively, you may use LocatorAutoCreate, which adds the oportunity of adding, moving and removing points "a piaccere":

DynamicModule[{pts = {{-1, 1}/2, {1, 1}/2}}, 
 Column[{LocatorPane[Dynamic[pts], Framed@Graphics[{}], LocatorAutoCreate -> All],
   Dynamic@pts}]]

enter image description here

Usage sample:

i = Import@ "http://worldmapsonline.com/images/OutlineMaps/Flags/Argentina2.jpg" 
DynamicModule[{pts = {{-1, 1}/2, {1, 1}/2}},
              LocatorPane[Dynamic[pts],
                   Show[{i, Graphics@Polygon@Dynamic@pts}, ImageSize -> {300, 300}],
              LocatorAutoCreate -> All]]

enter image description here

share|improve this answer
3  
+1 for the usage sample –  Rojo Jun 16 '12 at 22:58
    
@Rojo Somehow I knew I'll get at least one upvote b/c of that :) –  belisarius Jun 16 '12 at 23:00
add comment

Another way is to make use of the second argument of Dynamic. When given as a list of two functions, the first element of the second argument will be evaluated when the mouse is dragged, and the second one at the moment that the mouse is released, so in this case the the final value of the slider will be added to dataList.

pt = {1, 1};
dataList = {pt};
Slider2D[Dynamic[pt, {(pt = #) &, AppendTo[dataList, pt] &}], 
    {{0, 0}, {5, 8}, {1, 2}}]
share|improve this answer
    
+1 for reminding me to use the $f_{start}$ and $f_{end}$ arguments of Dynamic more often. –  István Zachar Jun 20 '12 at 8:11
add comment

Your Answer

 
discard

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

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