
Dear all, I tried to make an animation which draws Cardioid curve. I found a code which draws Cardioid using circles. Since I’d like to draw it differently, I modified it a little. 1. Define the z[] values in the \startMPdefinitions block 2. Modified the drawing method in \startMPbuffer When I run it, it doesn’t recognize the z[] values defined already. I think that the z[] values are not delivered to \startbuffer. I tried \startMPinclusions, \startMPinitialization, but I couldn’t succeed. 1. How can I deliver the z[] values into the buffer? 2. What is wrong in the following code? Thank you for reading. Best regards, Dalyoung \usemodule[animation] \setupinteraction[state=start] \startMPdefinitions%{Cardioid} path p,q; numeric u, ang,j,num; picture pict; u := 1cm; ang := 360/52; p := fullcircle scaled 5u; for i=0 upto 51: z[i] = 2.5*dir(ang*i)*u; endfor; pict := image ( draw p withpen pencircle scaled 1; ); \stopMPdefinitions \startbuffer[Cardioid][s] draw pict; num := \MPrawvar{Cardioid}{s}; for i = 0 upto num: j := 2i mod 52; draw z[i] -- z[j]; endfor; setbounds currentpicture to boundingbox currentpicture enlarged 2mm ; \stopbuffer \startdocument \startanimation[menu=yes, framerate=7] \dorecurse{52} {\startexpanded \startframe \setupMPvariables[Cardioid][s=\recurselevel] \processMPbuffer[Cardioid] \stopframe \stopexpanded} \stopanimation \stopdocument

Hi,
On Sat, Jul 19, 2025 at 2:42 AM Jeong Dal via ntg-context
Dear all,
I tried to make an animation which draws Cardioid curve. I found a code which draws Cardioid using circles. Since I’d like to draw it differently, I modified it a little.
1. Define the z[] values in the \startMPdefinitions block 2. Modified the drawing method in \startMPbuffer
When I run it, it doesn’t recognize the z[] values defined already. I think that the z[] values are not delivered to \startbuffer. I tried \startMPinclusions, \startMPinitialization, but I couldn’t succeed.
1. How can I deliver the z[] values into the buffer? 2. What is wrong in the following code?
Thank you for reading.
Best regards,
Dalyoung
\usemodule[animation] \setupinteraction[state=start]
\startMPdefinitions%{Cardioid} path p,q; numeric u, ang,j,num; picture pict;
u := 1cm; ang := 360/52;
p := fullcircle scaled 5u; for i=0 upto 51: z[i] = 2.5*dir(ang*i)*u; endfor; pict := image ( draw p withpen pencircle scaled 1; ); \stopMPdefinitions
\startbuffer[Cardioid][s] draw pict; num := \MPrawvar{Cardioid}{s};
for i = 0 upto num: j := 2i mod 52; draw z[i] -- z[j]; endfor; setbounds currentpicture to boundingbox currentpicture enlarged 2mm ; \stopbuffer
\startdocument
\startanimation[menu=yes, framerate=7] \dorecurse{52} {\startexpanded \startframe \setupMPvariables[Cardioid][s=\recurselevel] \processMPbuffer[Cardioid] \stopframe \stopexpanded} \stopanimation \stopdocument
I don't have the animation module, so maybe I miss the point, but the code below at least compiles and gives something that could be what you are looking for. If not, sorry for the noise. % \usemodule[animation] \setupinteraction[state=start] \startMPinitializations% CHANGED path p,q; numeric u, ang,j,num; picture pict; u := 1cm; ang := 360/52; p := fullcircle scaled 5u; for i=0 upto 51: z[i] = 2.5*dir(ang*i)*u; endfor; z[52]=z[0] ; % NEW pict := image ( draw p withpen pencircle scaled 1; ); \stopMPinitializations% CHANGED \startbuffer[Cardioid][s] draw pict; num := \MPrawvar{Cardioid}{s}; for i = 0 upto num: j := 2i mod 52; draw z[i] -- z[j]; endfor; setbounds currentpicture to boundingbox currentpicture enlarged 2mm ; \stopbuffer \startdocument % \startanimation[menu=yes, framerate=7] \dorecurse{52} {%\startexpanded \startframed \setupMPvariables[Cardioid][s=\recurselevel] \processMPbuffer[Cardioid] \stopframed %\stopexpanded } % \stopanimation \stopdocument /Mikael

Dear Mikael, I modified the code following your suggestion, and it worked. Thanks. After that I load animation module and activate the \startanimation … \stopanimation. But the output is a series of 52 figures not one figure. There is something more to make an animation. I have to figure it out. Anyway, one big problem is solved. Thanks again. Best regards, Dalyoung

On Sat, 19 Jul 2025, Jeong Dal via ntg-context wrote:
Dear Mikael,
I modified the code following your suggestion, and it worked.
Thanks.
After that I load animation module and activate the \startanimation … \stopanimation. But the output is a series of 52 figures not one figure.
There is something more to make an animation. I have to figure it out.
For the animation module, you need \startframe ... \stopframe. Mikael had changed it to \startframed ... \stopframed. I no longer use animation module, because the animations only work in acrobat, but in the past I had written a wrapper around the module to simplify metapost animations. https://adityam.github.io/context-blog/post/metapost-animation/ Aditya
participants (3)
-
Aditya Mahajan
-
Jeong Dal
-
Mikael Sundqvist