How do you create draggable points inside a graphic image?

3

I want to create a simple line with two points that can be dragged (one at a time), as in image below:

enter image description here

How can I do that without using sliders, but rather dragging the points themselves. I suspect that one uses Manipulate and Graphics in some fashion but I am not sure.

Bob Ueland

Posted 2019-07-11T18:19:09.097

Reputation: 999

Answers

4

Manipulate[
 Graphics[Line[{pt1, pt2}], PlotRange -> 2], {{pt1, {0, 0}}, 
  Locator}, {{pt2, {0, 1}}, Locator}]

Rohit Namjoshi

Posted 2019-07-11T18:19:09.097

Reputation: 7 759

8

Here is how you would do it with LocatorPane:

pt1 = {0, 0};
pt2 = {0, 2};
LocatorPane[
 Dynamic[{pt1, pt2}],
 Dynamic@Graphics[{
    Line[{pt1, pt2}]
    },
   PlotRange -> {{-5, 5}, {-5, 5}}
   ],
 Appearance -> {
   Graphics[{Red, PointSize[Large], Point[{0, 0}]}],
   Graphics[{Blue, PointSize[Large], Point[{0, 0}]}]
   }
 ]

enter image description here

C. E.

Posted 2019-07-11T18:19:09.097

Reputation: 67 448

4

You can also use a single control for multiple locators:

Manipulate[Graphics[Line[pts], PlotRange -> 2], 
   {{pts, {{0, 0}, {1, 1}}}, Locator, Appearance -> (Style["‚óŹ", #, 16] & /@ {Red, Green})}]

enter image description here

kglr

Posted 2019-07-11T18:19:09.097

Reputation: 302 076