# How draw a line dynamically using manipulate?

I want to paint a curve on a sphere while I’m manipulating stereoscopic projection points on the plane with a 2D slider, but I’m having trouble collecting the points needed to create the Line primitives. The example script merely tries to collect some points and draw a line on the same plane. The two lines pertaining to collecting the points and plotting the line are commented out because they produce looping or deep recursion. I’ve tried Append[] and Reap[Sow[]] in a number of rearrangements. Nothing turned up for me in searches, but if you know the right vocabulary for a query, you probably already know the solution. Any ideas?

Clear["Global*"]
Manipulate[
(*curve = Append[curve, pt];*)

Graphics[{
{Red,PointSize[.02],Point[pt]}
(*Line[curve]*)
},
Axes->True,
AxesLabel->{Style["x",Bold,16],Style["y",Bold,16]},
PlotRange->{{-1,1},{-1,1}},
Background->White,ImageSize->Medium],

{{pt,{0.0,0.0},Style["Move the point on the plane.",Bold,16]},{-1,-1},{1,1}},

Initialization:> (curve ={{0.0,0.0}})
]

• Use the option TrackedSymbols->{pt}? – kglr Aug 18 '14 at 21:49
• Perhaps you want locators with LocatorAutoCreate – Dr. belisarius Aug 18 '14 at 21:49
• What do you mean looping/deep recursion? It seems to work fine for me. – C. E. Aug 18 '14 at 21:59
• @Pickett, does it work fine uncommented? I want a curve to follow what is now a point. – Gary Palmer Aug 18 '14 at 22:17
• @GaryPalmer Yes, that is exactly what happened for me when I uncommented your code. Although you need a comma before the Line[curve]. – C. E. Aug 18 '14 at 22:21

This can be done more cleanly by localizing curve to the manipulate, which in turn is done by making curve an invisible control. This also obviates the need for the Initialization option, because controls take optional initializers.

Manipulate[
Graphics[{
{Red, PointSize @ .02, Point@pt},
Line @ AppendTo[curve, pt]},
Axes -> True,
AxesLabel -> {Style["x", Bold, 16], Style["y", Bold, 16]},
PlotRange -> {{-1, 1}, {-1, 1}},
ImageSize -> Medium],
{{pt, {0.0, 0.0}, Style["Move the point on the plane.", Bold, 16]}, {-1, -1}, {1, 1}},
{{curve, {{0.0, 0.0}}}, None},
TrackedSymbols -> {pt}]


• Thank you for bringing me this fact in mind again, I forgot to try it.(+) – hieron Aug 19 '14 at 1:56

Replacing the Set in Initialization by a SetDelayed solved the problem, TrackedSymbols should be set also.

Conjecture: It seems Clear before Manipulate produces problems, I can't believe it. It can be used in a separate cell, but not in the same cell. Experiences?

Manipulate[

curve = Append[curve, pt];
Graphics[{{Red, PointSize@.02, Point@pt}, Line@curve},
Axes -> True,
AxesLabel -> {Style["x", Bold, 16], Style["y", Bold, 16]},
PlotRange -> {{-1, 1}, {-1, 1}},
Background -> White,
ImageSize -> Medium],

{{pt, {0.0, 0.0},
Style["Move the point on the plane.", Bold, 16]}, {-1, -1}, {1, 1}},
Initialization :> (curve := {{0.0, 0.0}}) (* := *),
TrackedSymbols -> {pt}]


• as with Pickett's experience and with TrackedSymbols I no longer see a problem. SetDelayed actually breaks it. – Gary Palmer Aug 19 '14 at 0:32
• did you see how I simplified your first line in Graphics, using the @ symbol makes the code easy readable, avoiding brackets and parenthesis is a good strategy, but needs a good knowledge on syntax too. Clear is no more necessary. TrackedSymbols` is good, but not necessary. – hieron Aug 19 '14 at 0:41
• sorry I missed a ")" corrected now – hieron Aug 19 '14 at 0:48
• The Point[{pt[[1]],pt[[2]]}] rather than Point[pt] resulted from paring down a 3D script. It is also fixed. – Gary Palmer Aug 19 '14 at 1:09
• What MMA version do you use? – hieron Aug 19 '14 at 1:11