2

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?

2
3

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!).

enter image description here

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[0],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

enter image description here

// 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[0],blue);
label("$d = $ "+string(d),(-a+.2, -a+.2),align=E);
A.add();
restore();
}
erase();
A.movie();
8
  • 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?
    – kelalaka
    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.
    – Black Mild
    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);
    – kelalaka
    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!
    – Black Mild
    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[0],c[d]); label(format("d = ",d),(0, -1.25),align=(0,0)); } shipout(bbox(5mm,invisible));
    – Black Mild
    Feb 10 '21 at 18:01
1

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[0]);

    A.add(BBox(1cm, nullpen));
}
erase();
A.movie(delay=240);

Here is the animation for Edwards curves;

enter image description here

1
  • 1
    I'm open to any other ideas.
    – kelalaka
    Feb 6 '21 at 23:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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