3
$\begingroup$

How can I work with tables/lists of Locators under manipulate?

Note this is very similar to this question:

https://mathematica.stackexchange.com/a/51956/2079

The closest I've come is this:

 n = 3
 Manipulate[
    Graphics[{Circle[{0, 0}], Line[{{0, 0}, a[1]}]}],
    Sequence @@ 
      Map[{{a[#], RandomReal[{-1, 1}, {2}]}, Locator} & ,
           Range[n]] // Evaluate]

enter image description here

However any attempt to referece a by a variable index fails:

 n = 3
 Manipulate[
 Graphics[{Circle[{0, 0}], Map[Line[{{0, 0}, a[#]}] &, Range[n]]}],
 Sequence @@ 
   Map[{{a[#], RandomReal[{-1, 1}, {2}]}, Locator} & ,
     Range[n]] // Evaluate]

"coordinate a[1] should be a pair of numbers.. "

My best attempt at following the approach in the linked answer also fails:

 n = 3
 Manipulate[
    Graphics[Circle[{0, 0}]],
    {{data, RandomReal[{0, 1}, {n, 2}]}, ControlType -> None},
 Sequence @@ 
    Map[{{Dynamic[data[[#]]], RandomReal[{-1, 1}, {2}]}, Locator} & , 
    Range[n]] // Evaluate]

"Manipulate argument {{data[[1]],{-0.864708,0.897225}},Locator} does not have the correct form for a variable specification."

This came up working on this: https://mathematica.stackexchange.com/a/80099/2079 by the way.

$\endgroup$
2
  • 1
    $\begingroup$ First you should read the section on locators in this Documentation Center article. It discusses using multiple locator in a Manipulate expression. Second, you should consider whether a simple LocatorPane will be a better solution than Manipulate expression as solution to your problem. $\endgroup$
    – m_goldberg
    Commented Apr 20, 2015 at 15:22
  • $\begingroup$ ah! thanks @m_goldberg. The answer lies in creating a single locator call of the form { pts , list_of_initial_points }, Locator}, rather than a sequence of separate Locator calls { { point1 ,init1} , Locator } ,{ { point2 ,init2} , Locator } .. .. $\endgroup$
    – george2079
    Commented Apr 20, 2015 at 18:51

2 Answers 2

5
$\begingroup$
n = 3;
Manipulate[Graphics[{Circle[{0, 0}],Line[{{0, 0}, a[[#]]} & /@Range[Length@a]]}],
 {{a, RandomReal[{0, 1}, {n, 2}]}, Locator, LocatorAutoCreate -> True}]

enter image description here

Alternatively,

Manipulate[Graphics[{Circle[{0, 0}], Line[Tuples[{{{0, 0}}, a}]]}],
 {{a, RandomReal[{0, 1}, {n, 2}]}, Locator, LocatorAutoCreate -> True}]
$\endgroup$
1
$\begingroup$

for completeness here is the LocatorPane version suggested by @m_goldberg comment.

 DynamicModule[{pt = RandomReal[{-1, 1}, {3, 2}]},
   LocatorPane[Dynamic@pt,
     Graphics[{Circle[],
        Line[{{0, 0}, Dynamic@pt[[#]]}] & /@ Range@Length@pt}]]]
$\endgroup$

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