3
$\begingroup$

I use this Manipulate to generate a dynamic graphic:

Manipulate[pntx2=First[pt2]; pnty2=Last[pt2];
         Graphics[{{Red, Circle[]},
        {Thick, Line[{{0, 0}, pt2}]},
        {Point[{{0,pnty2}}]},
        {Dashed, Line[{{0,pnty2}, pt2},VertexColors->{Black, Black}]},
        {Thickness[0.015], Line[{{0,0},{0,pnty2}},VertexColors->{Cyan, Cyan}]}},
        Axes -> True, PlotRange -> 3/2],
        {{pt2, {1, 1}/Sqrt[2]}, Locator, TrackingFunction ->((pt2 = Normalize[#]) &)}]

Can someone explain me what does ((pt2 = Normalize[#]) &) exactly mean?

$\endgroup$
1
  • 2
    $\begingroup$ To expand further, it's a question of precedence: (pt2 = Normalize[#] &) is equivalent to (pt2 = (Normalize[#] &)). This would set pt2 to the function Normalize[#] & (just once, when the TrackingFunction option was evaluated). Since the value of var = rhs is rhs, the option would be effectively the same as TrackingFunction -> (Normalize[#] &), which would disable the movement of the Locator, since it does not update pt2 whenever the Locator is moved. $\endgroup$
    – Michael E2
    Jun 6, 2017 at 22:54

1 Answer 1

3
$\begingroup$

pt2 is the point where your mouse tries to drag the traker (the small black cross & circle).

((pt2 = Normalize[#]) &) means that before being updated, the vector representing the point where you tried to drag the traker is normalized. This way the tracker remains on the red ring, for it was deffined having raius 1 by default.

If you deffined the ring with radius 2 you would need to change this line to ((pt2 = 2 Normalize[#]) &).

Also it would be good to update the starting position: {pt2, 2 {1, 1}/Sqrt[2]}

A complete example:

Manipulate[pntx2 = First[pt2]; pnty2 = Last[pt2];
 Graphics[{{Red, Circle[{0, 0}, 2]}, {Thick, 
    Line[{{0, 0}, pt2}]}, {Point[{{0, pnty2}}]}, {Dashed, 
    Line[{{0, pnty2}, pt2}, 
     VertexColors -> {Black, Black}]}, {Thickness[0.015], 
    Line[{{0, 0}, {0, pnty2}}, VertexColors -> {Cyan, Cyan}]}}, 
  Axes -> True, PlotRange -> 5/2], {{pt2, 2 {1, 1}/Sqrt[2]}, Locator, 
  TrackingFunction -> ((pt2 = 2 Normalize[#]) &)}]
$\endgroup$
2
  • $\begingroup$ @Kuba, sorry man, I rolled back your edit for we both edited at the same time... $\endgroup$
    – ivbc
    Jun 6, 2017 at 20:42
  • $\begingroup$ No worries ;) Thanks for taking your time to answer. $\endgroup$
    – Kuba
    Jun 7, 2017 at 6:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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