Asymptote: Sending a parameter to function in contour

I want to produce an animation of a graph with Asymptote. To achieve this the function needs a parameter d;

real f(real x, real y) {

return (x^2 + y^2 + d * x);
}

guide[][] thegraphs = contour(f, a=(-2,-2), b=(2,2), new real[] {0});

How we can send a parameter using the contour?

Yes, in Asymptote there are so-called anonymous functions (see here, page 290) can be created with the keyword new, so that function definition in your example could be simplified without using programming tricks (you are difficult ^^ your trick is quite comfortable!). size(5cm);
import graph;
import contour;

typedef real function(real, real);
function f(real d) {
return new real(real x, real y) {
return x^2 + y^2 -1+ d*x^2*y^2;
};
}

pen[] c={red, blue,purple,orange}; c.cyclic=true; // for different Edwards curves

for(int d=100; d > 0; d -= 10) {
guide[][] thegraphs = contour(f(d), a=(-2,-2), b=(2,2), new real[] {0});
draw(thegraphs,c[d]);
}

shipout(bbox(5mm,invisible));

I hope you can adapt this to your animation. By the way, I am very impressive with recently-discovered Edwards curves and their application in cryptography.

Asymptote is rich in mathematical flavor!

Update Animation version // x.asy >>> x.pdf >>> making GIF ưith ImageMagick command in the command line window
// magick -density 200 x.pdf -alpha remove x.gif
unitsize(2cm);
import contour;
import animate;

typedef real function(real, real);
function f(real d) {
return new real(real x, real y) {
return x^2 + y^2 -1+ d*x^2*y^2;
};
}
real a=1.25;
draw(box((a,a),(-a,-a)),invisible);
draw((a,0)--(-a,0)^^(0,a)--(0,-a),gray);
animation A;

for(real d=360; d > -1; d -= 5) {
save();
guide[][] Edwards = contour(f(d), a=(-1,-1), b=(1,1), new real[] {0}, 200);
draw(Edwards,blue);
label("\$d = \$ "+string(d),(-a+.2, -a+.2),align=E);
restore();
}
erase();
A.movie();
• Thanks, definitely I will try. I wonder which one is faster ( I will test the speed). I couldn't solve the flickering text for d values. Any explanation? Is this a part of a book? Feb 10 '21 at 17:41
• @kelalaka What is the flickering text? I don't get it. If you describe in more details, I will try helping when I am free. The speed seems not so fast when increasing smoothness. Feb 10 '21 at 17:46
• I've put the current d value of the curve on the bottom left, if you wait around the twenties you will see that the position changes. Is is just label(format("d = %4.1f",i),(-1.25, -1.25),align=E); Feb 10 '21 at 17:48
• @kelalaka an usual trick: please try draw(box((2,2),(-2,-2)),invisible); to make bounding box fixed, so the animation will be fine! Feb 10 '21 at 17:51
• @kelalaka something like this? size(5cm); import graph; import contour; typedef real function(real, real); function f(real d) { return new real(real x, real y) { return x^2 + y^2 -1+ d*x^2*y^2; }; } pen[] c={red, blue,purple,orange}; c.cyclic=true; // for different Edwards curves draw(box((-1.5,1.5),(1.5,-1.75)),gray); for(int d=10; d > 0; d -= 10) { guide[][] thegraphs = contour(f(d), a=(-2,-2), b=(2,2), new real[] {0}); draw(thegraphs,c[d]); label(format("d = ",d),(0, -1.25),align=(0,0)); } shipout(bbox(5mm,invisible)); Feb 10 '21 at 18:01

I've solved my issue with programming tricks as;

real g(real x, real y, real d) {

return (x^2 + y^2 + d * x);
}

animation A=animation(global=false);
for(int i=300; i != 1; i -= 1) {

erase();

real f(real x, real y) {

return g(x, y, i);
}

guide[][] thegraphs = contour(f, a=(-2,-2), b=(2,2), new real[] {0});

draw(thegraphs); 