# How can I animate the polar graph of Trisectrix of Maclaurin?

From my previous question, this is my code from g.kov's answer:

``````import graph;
import math;
import animate;
settings.tex="pdflatex";

size(300);
real a=2;

animation Anim;
real f(real t){
return a/2*(4*cos(t)-1/cos(t));
}

xaxis(xmin=-2,xmax=4,Ticks(Step=1,modify=NoZero,pTick=invisible));
yaxis(ymin=-3,ymax=3,Ticks(Step=1,modify=NoZero,pTick=invisible));

real dtheta=0.25;
int n=500;
guide g=polargraph(f,-pi/2+dtheta,pi/2-dtheta,n,operator ..);
draw(g,dashed);

for (int i=0; i<360; ++i){
save();
drawline((0,0),dir(i));
drawline((a,0),(a,0)+dir(3*i));
restore();
}
erase();
Anim.movie(BBox(2mm,invisible));
``````

The output

I want to get this red curve like this:

In this case, we already know the parameter equation of the tracing curve, so just draw it in each frame of the animation. For simplicity, the following is an animation of a point on a circle (it looks like our Earth is rotating around the Sun).

``````// save as x.asy,
// compile with asy x.asy to get x.pdf in several pages
// then using this ImageMagick command:
// magick -density 200 x.pdf -alpha remove x.gif

import graph;
import animate;
unitsize(1cm);
real a=3;
animation Anim;
real x(real t){return a*cos(t);}
real y(real t){return a*sin(t);}

guide g=graph(x,y,0,2*pi,operator ..);
draw(g,dashed+gray+linewidth(.2pt));
fill(unitcircle,orange);

for (int i=0; i<360; i=i+2){
save();
draw(gtracing,red+linewidth(1pt));
restore();
}
erase();
Anim.movie(BBox(2mm,invisible));
``````

Update: Now adapting to the situation of OP. To control the flow of animation, I construct a suitable sequence for moving angles avoiding i=90 and i=270 where cos(i)=0.

``````int istep=2;
int[] arrangle;
for (int i=0; i<90; i=i+istep)
arrangle.push(i);
for (int i=90+dtheta; i<180; i=i+istep)
arrangle.push(i);
for (int i=180; i<270; i=i+istep)
arrangle.push(i);
for (int i=270+dtheta; i<360; i=i+istep)
arrangle.push(i);
``````

The complete code

``````import math;
import graph;
import animate;
unitsize(1cm);
real a=2;
animation Anim;
real f(real t){
return a/2*(4*cos(t)-1/cos(t));
}

xaxis(xmin=-2.5,xmax=4.5,Ticks(Step=1,modify=NoZero,pTick=invisible));
yaxis(Ticks(Step=1,modify=NoZero,pTick=invisible));

int dtheta=2; // in degrees
int n=100;    // smooth
draw(g,dashed);

// spiral for moving angles
pair spiral(real t){ // t is in degree

// construct sequence for moving angles
// avoiding i=90 and i=270 where cos(i)=0
int istep=2;
int[] arrangle;
for (int i=0; i<90; i=i+istep)
arrangle.push(i);
for (int i=90+dtheta; i<180; i=i+istep)
arrangle.push(i);
for (int i=180; i<270; i=i+istep)
arrangle.push(i);
for (int i=270+dtheta; i<360; i=i+istep)
arrangle.push(i);

for (int i : arrangle){
save();
draw(gtracing,.5red+.5white+linewidth(1pt));
draw(arc((0,0),.5,0,i),blue);
drawline((0,0),dir(i));
guide stracing=graph(spiral,0,3*i,operator ..);
draw(shift(a,0)*stracing,blue);

drawline((a,0),(a,0)+dir(3*i));
clip(box((-2.5,-3.5),(4.5,3.5)));
restore();
}

erase();
Anim.movie(BBox(2mm,invisible));
``````

Update 2 (update some code as OP requested) Tracing animation without knowing (parameter) equation.

Knowing (parameter) equation is just one way of determination of moving points. In case there is other way to get the position of moving points then we can connect all moving points from the beginning to the current position.

Again for simplicity, use the first example. First define an array of moving point

``````pair[] arrMovingPoints;
``````

Now in the loop, after getting the moving point `tmp`, we push it in the array `arrMovingPoints`, then draw the moving curve through all points in `M` with the joining syntax `operator..(...arrMovingPoints)` (see this answer by @Charles Staats).

``````pair tmp=a*dir(i); // the moving point
arrMovingPoints.push(tmp);
draw(operator..(...arrMovingPoints));
``````

Below is the full code, there is no (parameter) equation at all.

``````import animate;
unitsize(1cm);
real a=3;
animation Anim;
pair[] arrMovingPoints;
fill(unitcircle,purple);

for (int i=0; i<360; i=i+2){
save();
pair tmp=a*dir(i); // the moving point
arrMovingPoints.push(tmp);
draw(operator..(...arrMovingPoints),pink+1);
fill(circle(tmp,.2),red);