3
$\begingroup$

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.

$\endgroup$
4
$\begingroup$
Manipulate[
 Graphics[Line[{pt1, pt2}], PlotRange -> 2], {{pt1, {0, 0}}, 
  Locator}, {{pt2, {0, 1}}, Locator}]
$\endgroup$
8
$\begingroup$

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

$\endgroup$
4
$\begingroup$

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

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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