ntg-context - mailing list for ConTeXt users
 help / color / mirror / Atom feed
* Re: simplifying repeated macro calls in metafun
       [not found] <mailman.1069.1624285904.1130.ntg-context@ntg.nl>
@ 2021-06-21 21:50 ` Jeong Dal
  2021-06-22 13:49   ` Otared Kavian
  0 siblings, 1 reply; 15+ messages in thread
From: Jeong Dal @ 2021-06-21 21:50 UTC (permalink / raw)
  To: ntg-context


[-- Attachment #1.1: Type: text/plain, Size: 709 bytes --]


Dear Otared,

I received list mail in condensed form, I didn’t see your mail before I sent one.

As Mikael explained, “whatever” is very nice to find the intersection point of two lines.
You can see nice examples which use “whatever” in MetaFun manual.

In my code, the next point is the intersection point of the edge and the folding line which bisect the angle of the former folding line [M,N].

H := whatever[N, N + dir(.5*angle(M-N))*u] = whatever[A,B];

Enjoy the use of “whatever”!

I made an animation of the folding process using animation module and \processMPbuffer.
But Woldgang’s code showed a way to make it using \startuseMPgraphic.


Best regards,

Dalyoung

[-- Attachment #1.2: Type: text/html, Size: 2058 bytes --]

[-- Attachment #2: Type: text/plain, Size: 493 bytes --]

___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!

maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : http://www.pragma-ade.nl / http://context.aanhet.net
archive  : https://bitbucket.org/phg/context-mirror/commits/
wiki     : http://contextgarden.net
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: simplifying repeated macro calls in metafun
  2021-06-21 21:50 ` simplifying repeated macro calls in metafun Jeong Dal
@ 2021-06-22 13:49   ` Otared Kavian
  2021-06-22 16:00     ` Wolfgang Schuster
  0 siblings, 1 reply; 15+ messages in thread
From: Otared Kavian @ 2021-06-22 13:49 UTC (permalink / raw)
  To: mailing list for ConTeXt users


[-- Attachment #1.1: Type: text/plain, Size: 1817 bytes --]

Dear Dalyoung, dear Mikael

Thank you both for your explanations: indeed this « whatever » command in MetaPost is magic and very useful (I am not sure whether it is a command only in MetaFun, or it exists also in the original MetaPost).

Regarding the way Aditya uses \startuseMPgraphic, it is also very instructive. 
Dalyoung, can you also share the animation example for the foldings ? I am sure we are a few on the list to learn something…

Best regards: Otared

> On 21 Jun 2021, at 23:50, Jeong Dal <haksan@me.com> wrote:
> 
> 
> Dear Otared,
> 
> I received list mail in condensed form, I didn’t see your mail before I sent one.
> 
> As Mikael explained, “whatever” is very nice to find the intersection point of two lines.
> You can see nice examples which use “whatever” in MetaFun manual.
> 
> In my code, the next point is the intersection point of the edge and the folding line which bisect the angle of the former folding line [M,N].
> 
> H := whatever[N, N + dir(.5*angle(M-N))*u] = whatever[A,B];
> 
> Enjoy the use of “whatever”!
> 
> I made an animation of the folding process using animation module and \processMPbuffer.
> But Woldgang’s code showed a way to make it using \startuseMPgraphic.
> 
> 
> Best regards,
> 
> Dalyoung
> ___________________________________________________________________________________
> If your question is of interest to others as well, please add an entry to the Wiki!
> 
> maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
> webpage  : http://www.pragma-ade.nl / http://context.aanhet.net
> archive  : https://bitbucket.org/phg/context-mirror/commits/
> wiki     : http://contextgarden.net
> ___________________________________________________________________________________


[-- Attachment #1.2: Type: text/html, Size: 4124 bytes --]

[-- Attachment #2: Type: text/plain, Size: 493 bytes --]

___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!

maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : http://www.pragma-ade.nl / http://context.aanhet.net
archive  : https://bitbucket.org/phg/context-mirror/commits/
wiki     : http://contextgarden.net
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: simplifying repeated macro calls in metafun
  2021-06-22 13:49   ` Otared Kavian
@ 2021-06-22 16:00     ` Wolfgang Schuster
  0 siblings, 0 replies; 15+ messages in thread
From: Wolfgang Schuster @ 2021-06-22 16:00 UTC (permalink / raw)
  To: mailing list for ConTeXt users; +Cc: Otared Kavian


[-- Attachment #1.1: Type: text/plain, Size: 375 bytes --]

Otared Kavian schrieb am 22.06.2021 um 15:49:
> Dear Dalyoung, dear Mikael
>
> Thank you both for your explanations: indeed this « whatever » command 
> in MetaPost is magic and very useful (I am not sure whether it is a 
> command only in MetaFun, or it exists also in the original MetaPost).

You have to go further back, whatever already existed in MetaFont.

Wolfgang


[-- Attachment #1.2: Type: text/html, Size: 882 bytes --]

[-- Attachment #2: Type: text/plain, Size: 493 bytes --]

___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!

maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : http://www.pragma-ade.nl / http://context.aanhet.net
archive  : https://bitbucket.org/phg/context-mirror/commits/
wiki     : http://contextgarden.net
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: simplifying repeated macro calls in metafun
       [not found] <mailman.47.1624523134.1143.ntg-context@ntg.nl>
@ 2021-06-25 19:14 ` Jeong Dal
  0 siblings, 0 replies; 15+ messages in thread
From: Jeong Dal @ 2021-06-25 19:14 UTC (permalink / raw)
  To: list ntg-context@ntg.nl ntg-context@ntg.nl ntg-context@ntg.nl
	ntg-context@ntg.nl

Dear Aditya,


> A while back, I wrote some code to easily generate metapost animations. 
> 
> https://adityam.github.io/context-blog/post/metapost-animation/
> 
> I haven't used it much because I am mostly on linux, and its is a PITA to get an older version of Adobe Reader to work under wine. 

I have visited this site before, and I have used it for a few time in similar cases.
Thank you for a very nice code.

Best regards,

Dalyoung
___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!

maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : http://www.pragma-ade.nl / http://context.aanhet.net
archive  : https://bitbucket.org/phg/context-mirror/commits/
wiki     : http://contextgarden.net
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: simplifying repeated macro calls in metafun
  2021-06-24  3:42     ` Aditya Mahajan
@ 2021-06-24  7:16       ` Hans Hagen
  0 siblings, 0 replies; 15+ messages in thread
From: Hans Hagen @ 2021-06-24  7:16 UTC (permalink / raw)
  To: mailing list for ConTeXt users, Aditya Mahajan

On 6/24/2021 5:42 AM, Aditya Mahajan wrote:
> On Wed, 23 Jun 2021, Aditya Mahajan wrote:
> 
>> On Thu, 24 Jun 2021, Jeong Dal wrote:
>>
>>> Now, I am trying to do the same thing using \startuseMPgraphic, but I don’t the correct result yet.
>>> Using \startuseMPgraphic, and \dorecurse, the code is simpler. But, it seems to me that \startanimation … \stopanimation is not working with \dorecurse{}{} …
>>> It just list all the graphics.
>>> Any hint is welcome.
>>
>> A while back, I wrote some code to easily generate metapost animations.
>>
>> https://adityam.github.io/context-blog/post/metapost-animation/
>>
>> I haven't used it much because I am mostly on linux, and its is a PITA to get an older version of Adobe Reader to work under wine.
> 
> Here's a version using these macros. It runs with MkIV but not with LMTX. Sorry, no time to debug what's going wrong in LMTX right now.
mp variable handling in lmtx is a bit more sophisticated

when we pass colors, numerics, dimensions there is some guess work going 
on wrt the type of the variable and on the average that works ok but in 
lmtx we can also fetch variables

so here is a basic setup for you (not much animation but it shows the idea

\unprotect

% this is similar to mkiv, although we add protection against
% overload; i should  still use the new argument handling here

\permanent\protected\def\startMPanimation
   {\dodoublegroupempty\meta_start_animation}

\permanent\protected\lettonothing\stopMPanimation

\def\meta_start_animation#1%
   {\normalexpanded{\meta_start_animation_indeed{#1}}}

\permanent\protected\def\meta_start_animation_indeed#1#2#3\stopMPanimation
 
{\gdefcsname\??mpgraphic#1\endcsname{\meta_handle_use_graphic{#1}{#2}{#3}}}

% this one is like yours too but it has more wrapping inside
% wrt namespace, picking up variables etc; as you can see,
% we just operate in the normal mp related variable namespace

\permanent\tolerant\protected\def\useMPanimation#=#*#=%
   {\meta_begin_graphic_group{#1}%
    \checkmpcategoryparent
    \let\currentmpcategory\currentMPgraphicname
    \setupcurrentmpcategory[#2]%
    \dorecurse{\mpcategoryparameter{n}}{%
       \letmpcategoryparameter{i}\recurselevel
       \begincsname\??mpgraphic#1\endcsname\empty
    }%
    \meta_end_graphic_group}

% so, the main thing here is that we added the loop and
% for convenience i store the current step

\protect

\starttext

\setupMPvariables
   [animation]
   [n=4]

% watch how we *pick up* a variable, no expansion mess, we
% avoid heuristics, it might even be faster (not that it
% matters because tex is already pretty fast)

\startMPanimation{circle}
     fill fullcircle scaled (10cm / mpvarn("i"));
\stopMPanimation

% there currently are mpvarn, mpvard, mpvars, mpvarb

\useMPanimation{circle}{n=4}

\stoptext



-----------------------------------------------------------------
                                           Hans Hagen | PRAGMA ADE
               Ridderstraat 27 | 8061 GH Hasselt | The Netherlands
        tel: 038 477 53 69 | www.pragma-ade.nl | www.pragma-pod.nl
-----------------------------------------------------------------
___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!

maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : http://www.pragma-ade.nl / http://context.aanhet.net
archive  : https://bitbucket.org/phg/context-mirror/commits/
wiki     : http://contextgarden.net
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: simplifying repeated macro calls in metafun
  2021-06-24  3:31   ` Aditya Mahajan
@ 2021-06-24  3:42     ` Aditya Mahajan
  2021-06-24  7:16       ` Hans Hagen
  0 siblings, 1 reply; 15+ messages in thread
From: Aditya Mahajan @ 2021-06-24  3:42 UTC (permalink / raw)
  To: mailing list for ConTeXt users

[-- Attachment #1: Type: text/plain, Size: 864 bytes --]

On Wed, 23 Jun 2021, Aditya Mahajan wrote:

> On Thu, 24 Jun 2021, Jeong Dal wrote:
> 
> > Now, I am trying to do the same thing using \startuseMPgraphic, but I don’t the correct result yet.
> > Using \startuseMPgraphic, and \dorecurse, the code is simpler. But, it seems to me that \startanimation … \stopanimation is not working with \dorecurse{}{} …
> > It just list all the graphics.
> > Any hint is welcome.
> 
> A while back, I wrote some code to easily generate metapost animations. 
> 
> https://adityam.github.io/context-blog/post/metapost-animation/
> 
> I haven't used it much because I am mostly on linux, and its is a PITA to get an older version of Adobe Reader to work under wine. 

Here's a version using these macros. It runs with MkIV but not with LMTX. Sorry, no time to debug what's going wrong in LMTX right now.

Aditya

[-- Attachment #2: Type: application/x-tex, Size: 3268 bytes --]

[-- Attachment #3: Type: text/plain, Size: 493 bytes --]

___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!

maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : http://www.pragma-ade.nl / http://context.aanhet.net
archive  : https://bitbucket.org/phg/context-mirror/commits/
wiki     : http://contextgarden.net
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: simplifying repeated macro calls in metafun
  2021-06-23 20:26 ` Jeong Dal
@ 2021-06-24  3:31   ` Aditya Mahajan
  2021-06-24  3:42     ` Aditya Mahajan
  0 siblings, 1 reply; 15+ messages in thread
From: Aditya Mahajan @ 2021-06-24  3:31 UTC (permalink / raw)
  To: mailing list for ConTeXt users

[-- Attachment #1: Type: text/plain, Size: 655 bytes --]

On Thu, 24 Jun 2021, Jeong Dal wrote:

> Now, I am trying to do the same thing using \startuseMPgraphic, but I don’t the correct result yet.
> Using \startuseMPgraphic, and \dorecurse, the code is simpler. But, it seems to me that \startanimation … \stopanimation is not working with \dorecurse{}{} …
> It just list all the graphics.
> Any hint is welcome.

A while back, I wrote some code to easily generate metapost animations. 

https://adityam.github.io/context-blog/post/metapost-animation/

I haven't used it much because I am mostly on linux, and its is a PITA to get an older version of Adobe Reader to work under wine. 

Aditya

[-- Attachment #2: Type: text/plain, Size: 493 bytes --]

___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!

maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : http://www.pragma-ade.nl / http://context.aanhet.net
archive  : https://bitbucket.org/phg/context-mirror/commits/
wiki     : http://contextgarden.net
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: simplifying repeated macro calls in metafun
       [not found] <mailman.1145.1624377609.1130.ntg-context@ntg.nl>
@ 2021-06-23 20:26 ` Jeong Dal
  2021-06-24  3:31   ` Aditya Mahajan
  0 siblings, 1 reply; 15+ messages in thread
From: Jeong Dal @ 2021-06-23 20:26 UTC (permalink / raw)
  To: list ntg-context@ntg.nl ntg-context@ntg.nl ntg-context@ntg.nl
	ntg-context@ntg.nl


[-- Attachment #1.1: Type: text/plain, Size: 5393 bytes --]

Dear Otared,

> 
> Regarding the way Aditya uses \startuseMPgraphic, it is also very instructive. 
> Dalyoung, can you also share the animation example for the foldings ? I am sure we are a few on the list to learn something…
> 

1. Here is the working sample of animation using \processMPbuffer which I did before. You can see it using adobe reader.


\startbuffer[UDBasic]
  pair A,B,C,D;
  pair E, F, G, H;
  path p,q;
  numeric ang,maxNum;
  u:=1in;
  A=(.95*TextWidth,u); B=(0,u); C=(0,0); D=(.95*TextWidth,0);
  p := A--B--C--D;
  q := (TextWidth,1.5u)--(0,1.5u)--(0,-.5u)--(TextWidth,-.5u)--cycle;
  z0=(.1u,0);
  
   vardef nextPt (expr M,N) = 
   if ypart(N) = 0: 
       H := whatever[N, N + dir(.5*angle(M-N))*u] = whatever[A,B];
   else:
       H := whatever[N, N + dir(.5*angle(M-N))*u] = whatever[C,D];
   fi;
   H
   enddef;

   def grayUD(expr i) =
      draw G -- if ypart(B)=ypart(G): B -- C else: C -- B fi --z[i];
      filldraw z[i]+2*(G-z[i])*u -- z[i] -- z[i+1] -- z[i+1]+(G-z[i])*u -- cycle withcolor .75white; 
      draw G -- z[i+1] dashed evenly;
      draw z[0] for k=0 upto i: --z[k] endfor dashed evenly;
      clip currentpicture to q;
      setbounds currentpicture to boundingbox q;
   enddef;

   def linesUD(expr i) =
      draw p;
      draw z0 for k=0 upto i+1: --z[k] endfor dashed evenly;
      setbounds currentpicture to boundingbox q;
   enddef;
\stopbuffer

\startbuffer[UDpts]
   ang := 40;
   z[1] = whatever[z[0], z[0]+dir(ang)*u] = whatever[A,B];
   G := whatever[z[0], z[0]+dir(2*ang)*u] = whatever[A,B];
   for k = 2 upto 20:
      z[k] = nextPt(z[k-2],z[k-1]);
      maxNum := k;
      exitif xpart(z[k]) > TextWidth;
   endfor;
\stopbuffer
\startbuffer[UD0]
   draw p;
   setbounds currentpicture to boundingbox q;
\stopbuffer
\startbuffer[UD1]
   grayUD(0);
\stopbuffer
\startbuffer[UD11]
   linesUD(0);
\stopbuffer
\startbuffer[UD2]
   G := z0;
   grayUD(1)
\stopbuffer
\startbuffer[UD21]
   linesUD(1);
\stopbuffer
\startbuffer[UD3]
   G := z1;
   grayUD(2);
\stopbuffer
\startbuffer[UD31]
   linesUD(2);
\stopbuffer
\startbuffer[UD4]
   G := z2;
   grayUD(3);
\stopbuffer
\startbuffer[UD41]
   linesUD(3);
\stopbuffer
\startbuffer[UD5]
   G := z3;
   grayUD(4);
\stopbuffer
\startbuffer[UD51]
   linesUD(4);
\stopbuffer
\startbuffer[UDFinal]
draw p;
draw z0--z1--z2--z3--z4--z5--z6--z7--z8 dashed evenly;
setbounds currentpicture to boundingbox q;
\stopbuffer

\setupinteraction[state=start]
\usemodule[animation]
\starttext
\startanimation[menu=yes]
{\processMPbuffer[UDBasic,UDpts,UD0]}
{\processMPbuffer[UDBasic,UDpts,UD1]}
{\processMPbuffer[UDBasic,UDpts,UD11]}
{\processMPbuffer[UDBasic,UDpts,UD2]}
{\processMPbuffer[UDBasic,UDpts,UD21]}
{\processMPbuffer[UDBasic,UDpts,UD3]}
{\processMPbuffer[UDBasic,UDpts,UD31]}
{\processMPbuffer[UDBasic,UDpts,UD4]}
{\processMPbuffer[UDBasic,UDpts,UD41]}
{\processMPbuffer[UDBasic,UDpts,UD5]}
{\processMPbuffer[UDBasic,UDpts,UD51]}
{\processMPbuffer[UDBasic,UDpts,UDFinal]}
\stopanimation
\stoptext

Now, I am trying to do the same thing using \startuseMPgraphic, but I don’t the correct result yet.
Using \startuseMPgraphic, and \dorecurse, the code is simpler. But, it seems to me that \startanimation … \stopanimation is not working with \dorecurse{}{} …
It just list all the graphics.
Any hint is welcome.

\startMPinitializations%{UDBasic}
  pair A,B,C,D;
  pair E, F, G, H;
  path p,q;
  numeric ang,maxNum;

  numeric n; %AM Added

  u:=1in;
  A=(.95*TextWidth,u); B=(0,u); C=(0,0); D=(.95*TextWidth,0);
  p := A--B--C--D;
  q := (TextWidth,1.5u)--(0,1.5u)--(0,-.5u)--(TextWidth,-.5u)--cycle;
  z0=(.1u,0);
  
   vardef nextPt (expr M,N) = 
   if ypart(N) = 0: 
       H := whatever[N, N + dir(.5*angle(M-N))*u] = whatever[A,B];
   else:
       H := whatever[N, N + dir(.5*angle(M-N))*u] = whatever[C,D];
   fi;
   H
   enddef;

   def grayUD(expr i) =
      draw G -- if ypart(B)=ypart(G): B -- C else: C -- B fi --z[i];
      filldraw z[i]+2*(G-z[i])*u -- z[i] -- z[i+1] -- z[i+1]+(G-z[i])*u -- cycle withcolor .75white; 
      draw G -- z[i+1] dashed evenly;
      draw z[0] for k=0 upto i: --z[k] endfor dashed evenly;
      clip currentpicture to q;
   enddef;

   def linesUD(expr i) =
      draw p;
      draw z0 for k=0 upto i+1: --z[k] endfor dashed evenly;

      % AM: removed. Set in in the main code
      % setbounds currentpicture to boundingbox q;
   enddef;

   ang := 40;
   z[1] = whatever[z[0], z[0]+dir(ang)*u] = whatever[A,B];

   % AM: Changed next line from G to z[-1]
   z[-1] = whatever[z[0], z[0]+dir(2*ang)*u] = whatever[A,B];
   for k = 2 upto 20:
      z[k] = nextPt(z[k-2],z[k-1]);
      maxNum := k;
      exitif xpart(z[k]) > TextWidth;
   endfor;

\stopMPinitializations

\startuseMPgraphic{foldUD}{num}
   numeric n;
   n := \MPvar{num}-1;
   G := z[n-1];
   grayUD(n);
\stopuseMPgraphic
\startuseMPgraphic{dotLines}{num}
   numeric n;
   n := \MPvar{num}-1;
   linesUD(n);
\stopuseMPgraphic

\setupinteraction[state=start]
\usemodule[animation]
\starttext
\startanimation[menu=yes]
\dorecurse{7}
{
 {\useMPgraphic{foldUD}{num=\recurselevel}}
 {\useMPgraphic{dotLines}{num=\recurselevel}}
}
\stopanimation
\stoptext

Best regards,

Dalyoung


[-- Attachment #1.2: Type: text/html, Size: 44998 bytes --]

[-- Attachment #2: Type: text/plain, Size: 493 bytes --]

___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!

maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : http://www.pragma-ade.nl / http://context.aanhet.net
archive  : https://bitbucket.org/phg/context-mirror/commits/
wiki     : http://contextgarden.net
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: simplifying repeated macro calls in metafun
  2021-06-21 14:31         ` Otared Kavian
@ 2021-06-21 18:35           ` Mikael Sundqvist
  0 siblings, 0 replies; 15+ messages in thread
From: Mikael Sundqvist @ 2021-06-21 18:35 UTC (permalink / raw)
  To: mailing list for ConTeXt users

Hi,

yes, sure, I should have copied my code from the geometry document
where I do this kind of constructions over and over...

/Mikael

On Mon, Jun 21, 2021 at 4:31 PM Otared Kavian <otared@gmail.com> wrote:
>
> Hi Mikael,
>
> Thank you so much for the explanation: I am ashamed for not having read this in the MetaPost manual.
>
> Just to make sure whether I am beginning to understand, in your example code
>
> > z3 = whatever[z1,z2] = z0+whatever*dir(angle(z1-z0)+90));
>
> should the second « whatever » be
>
> z0 + whatever*dir(angle(z2 - z1) + 90)) ;
>
> or am I proving that I have not yet understood this wonderful command ?
>
> Thanks again and best regards: Otared
>
> > On 21 Jun 2021, at 14:21, Mikael Sundqvist <mickep@gmail.com> wrote:
> >
> > Hi,
> >
> > As is written in the MetaPost manual: Whatever generates a new
> > anonymous variable each time it is used. It is very useful in cases
> > where you are not interested in the value of a variable, but want to
> > use it to (let MP) calculate something. Example
> >
> > you have three points z0, z1, z2 forming a triangle, and you want to
> > find where the altitude from z0 is intersecting the line between z1
> > and z2. You can find that intersection z3 by writing
> >
> > z3 = whatever[z1,z2] = z0+whatever*dir(angle(z1-z0)+90));
> >
> > The first equality says only that z3 should be on the line between z1
> > and z2, the second one says that z3 should be on the line through z0,
> > perpendicular to the line between z1 and z2. Since MetaPost solves
> > linear equations, this defines z3, and we need not to think more about
> > the actual values of the "whatevers" (note that they are not in
> > general equal in this example!).
> >
> > Hope it helps!
> >
> > /Mikael
> >
> >
> > On Mon, Jun 21, 2021 at 2:07 PM Otared Kavian <otared@gmail.com> wrote:
> >>
> >> Hi Aditya,
> >>
> >> Thanks for the detailed file you sent to answer Dalyoung’s question and file. And going through both files I have a question.
> >> I see that both of you use a function « whatever » (but is it a function ?): what does this do and where can it be used (actually I have seen Hans using this expression, but I had always thought this is just a name…
> >>
> >> Best regards: Otared
> >>
> >>> On 21 Jun 2021, at 06:26, Aditya Mahajan <adityam@umich.edu> wrote:
> >>>
> >>> On Mon, 21 Jun 2021, Jeong Dal wrote:
> >>>
> >>>>
> >>>> Hi,
> >>>>
> >>>> I defined a macro definition in metafun, and draw figures using \startbuffer[] … \stopbuffer and \processMPbuffer[].
> >>>> As you see in the attached code, the same macro definition is used repeatedly by changing the numeric argument.
> >>>> Is there a better way to simplify the code, for example, using \dorecurse and recurselevel?
> >>>
> >>> See attached. The main idea is to store the repetitive bits in \startuseMPgraphic{...} and then reuse them using \includeMPgraphic.
> >>>
> >>> Aditya<mp-test.tex>___________________________________________________________________________________
> >>> If your question is of interest to others as well, please add an entry to the Wiki!
> >>>
> >>> maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
> >>> webpage  : http://www.pragma-ade.nl / http://context.aanhet.net
> >>> archive  : https://bitbucket.org/phg/context-mirror/commits/
> >>> wiki     : http://contextgarden.net
> >>> ___________________________________________________________________________________
> >>
> >> ___________________________________________________________________________________
> >> If your question is of interest to others as well, please add an entry to the Wiki!
> >>
> >> maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
> >> webpage  : http://www.pragma-ade.nl / http://context.aanhet.net
> >> archive  : https://bitbucket.org/phg/context-mirror/commits/
> >> wiki     : http://contextgarden.net
> >> ___________________________________________________________________________________
> > ___________________________________________________________________________________
> > If your question is of interest to others as well, please add an entry to the Wiki!
> >
> > maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
> > webpage  : http://www.pragma-ade.nl / http://context.aanhet.net
> > archive  : https://bitbucket.org/phg/context-mirror/commits/
> > wiki     : http://contextgarden.net
> > ___________________________________________________________________________________
>
> ___________________________________________________________________________________
> If your question is of interest to others as well, please add an entry to the Wiki!
>
> maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
> webpage  : http://www.pragma-ade.nl / http://context.aanhet.net
> archive  : https://bitbucket.org/phg/context-mirror/commits/
> wiki     : http://contextgarden.net
> ___________________________________________________________________________________
___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!

maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : http://www.pragma-ade.nl / http://context.aanhet.net
archive  : https://bitbucket.org/phg/context-mirror/commits/
wiki     : http://contextgarden.net
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: simplifying repeated macro calls in metafun
  2021-06-21 12:21       ` Mikael Sundqvist
@ 2021-06-21 14:31         ` Otared Kavian
  2021-06-21 18:35           ` Mikael Sundqvist
  0 siblings, 1 reply; 15+ messages in thread
From: Otared Kavian @ 2021-06-21 14:31 UTC (permalink / raw)
  To: mailing list for ConTeXt users

Hi Mikael,

Thank you so much for the explanation: I am ashamed for not having read this in the MetaPost manual.

Just to make sure whether I am beginning to understand, in your example code

> z3 = whatever[z1,z2] = z0+whatever*dir(angle(z1-z0)+90));

should the second « whatever » be 

z0 + whatever*dir(angle(z2 - z1) + 90)) ;

or am I proving that I have not yet understood this wonderful command ?

Thanks again and best regards: Otared

> On 21 Jun 2021, at 14:21, Mikael Sundqvist <mickep@gmail.com> wrote:
> 
> Hi,
> 
> As is written in the MetaPost manual: Whatever generates a new
> anonymous variable each time it is used. It is very useful in cases
> where you are not interested in the value of a variable, but want to
> use it to (let MP) calculate something. Example
> 
> you have three points z0, z1, z2 forming a triangle, and you want to
> find where the altitude from z0 is intersecting the line between z1
> and z2. You can find that intersection z3 by writing
> 
> z3 = whatever[z1,z2] = z0+whatever*dir(angle(z1-z0)+90));
> 
> The first equality says only that z3 should be on the line between z1
> and z2, the second one says that z3 should be on the line through z0,
> perpendicular to the line between z1 and z2. Since MetaPost solves
> linear equations, this defines z3, and we need not to think more about
> the actual values of the "whatevers" (note that they are not in
> general equal in this example!).
> 
> Hope it helps!
> 
> /Mikael
> 
> 
> On Mon, Jun 21, 2021 at 2:07 PM Otared Kavian <otared@gmail.com> wrote:
>> 
>> Hi Aditya,
>> 
>> Thanks for the detailed file you sent to answer Dalyoung’s question and file. And going through both files I have a question.
>> I see that both of you use a function « whatever » (but is it a function ?): what does this do and where can it be used (actually I have seen Hans using this expression, but I had always thought this is just a name…
>> 
>> Best regards: Otared
>> 
>>> On 21 Jun 2021, at 06:26, Aditya Mahajan <adityam@umich.edu> wrote:
>>> 
>>> On Mon, 21 Jun 2021, Jeong Dal wrote:
>>> 
>>>> 
>>>> Hi,
>>>> 
>>>> I defined a macro definition in metafun, and draw figures using \startbuffer[] … \stopbuffer and \processMPbuffer[].
>>>> As you see in the attached code, the same macro definition is used repeatedly by changing the numeric argument.
>>>> Is there a better way to simplify the code, for example, using \dorecurse and recurselevel?
>>> 
>>> See attached. The main idea is to store the repetitive bits in \startuseMPgraphic{...} and then reuse them using \includeMPgraphic.
>>> 
>>> Aditya<mp-test.tex>___________________________________________________________________________________
>>> If your question is of interest to others as well, please add an entry to the Wiki!
>>> 
>>> maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
>>> webpage  : http://www.pragma-ade.nl / http://context.aanhet.net
>>> archive  : https://bitbucket.org/phg/context-mirror/commits/
>>> wiki     : http://contextgarden.net
>>> ___________________________________________________________________________________
>> 
>> ___________________________________________________________________________________
>> If your question is of interest to others as well, please add an entry to the Wiki!
>> 
>> maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
>> webpage  : http://www.pragma-ade.nl / http://context.aanhet.net
>> archive  : https://bitbucket.org/phg/context-mirror/commits/
>> wiki     : http://contextgarden.net
>> ___________________________________________________________________________________
> ___________________________________________________________________________________
> If your question is of interest to others as well, please add an entry to the Wiki!
> 
> maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
> webpage  : http://www.pragma-ade.nl / http://context.aanhet.net
> archive  : https://bitbucket.org/phg/context-mirror/commits/
> wiki     : http://contextgarden.net
> ___________________________________________________________________________________

___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!

maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : http://www.pragma-ade.nl / http://context.aanhet.net
archive  : https://bitbucket.org/phg/context-mirror/commits/
wiki     : http://contextgarden.net
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: simplifying repeated macro calls in metafun
       [not found] <mailman.1.1624183201.7245.ntg-context@ntg.nl>
  2021-06-20 23:30 ` Jeong Dal
@ 2021-06-21 13:34 ` Jeong Dal
  1 sibling, 0 replies; 15+ messages in thread
From: Jeong Dal @ 2021-06-21 13:34 UTC (permalink / raw)
  To: list ntg-context@ntg.nl ntg-context@ntg.nl ntg-context@ntg.nl
	ntg-context@ntg.nl

Dear Aditya,

Your code works perfectly.

Thank you for your help.

Best regards,

Dalyoung

___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!

maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : http://www.pragma-ade.nl / http://context.aanhet.net
archive  : https://bitbucket.org/phg/context-mirror/commits/
wiki     : http://contextgarden.net
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: simplifying repeated macro calls in metafun
  2021-06-21 12:07     ` Otared Kavian
@ 2021-06-21 12:21       ` Mikael Sundqvist
  2021-06-21 14:31         ` Otared Kavian
  0 siblings, 1 reply; 15+ messages in thread
From: Mikael Sundqvist @ 2021-06-21 12:21 UTC (permalink / raw)
  To: mailing list for ConTeXt users

Hi,

As is written in the MetaPost manual: Whatever generates a new
anonymous variable each time it is used. It is very useful in cases
where you are not interested in the value of a variable, but want to
use it to (let MP) calculate something. Example

you have three points z0, z1, z2 forming a triangle, and you want to
find where the altitude from z0 is intersecting the line between z1
and z2. You can find that intersection z3 by writing

z3 = whatever[z1,z2] = z0+whatever*dir(angle(z1-z0)+90));

The first equality says only that z3 should be on the line between z1
and z2, the second one says that z3 should be on the line through z0,
perpendicular to the line between z1 and z2. Since MetaPost solves
linear equations, this defines z3, and we need not to think more about
the actual values of the "whatevers" (note that they are not in
general equal in this example!).

Hope it helps!

/Mikael


On Mon, Jun 21, 2021 at 2:07 PM Otared Kavian <otared@gmail.com> wrote:
>
> Hi Aditya,
>
> Thanks for the detailed file you sent to answer Dalyoung’s question and file. And going through both files I have a question.
> I see that both of you use a function « whatever » (but is it a function ?): what does this do and where can it be used (actually I have seen Hans using this expression, but I had always thought this is just a name…
>
> Best regards: Otared
>
> > On 21 Jun 2021, at 06:26, Aditya Mahajan <adityam@umich.edu> wrote:
> >
> > On Mon, 21 Jun 2021, Jeong Dal wrote:
> >
> >>
> >> Hi,
> >>
> >> I defined a macro definition in metafun, and draw figures using \startbuffer[] … \stopbuffer and \processMPbuffer[].
> >> As you see in the attached code, the same macro definition is used repeatedly by changing the numeric argument.
> >> Is there a better way to simplify the code, for example, using \dorecurse and recurselevel?
> >
> > See attached. The main idea is to store the repetitive bits in \startuseMPgraphic{...} and then reuse them using \includeMPgraphic.
> >
> > Aditya<mp-test.tex>___________________________________________________________________________________
> > If your question is of interest to others as well, please add an entry to the Wiki!
> >
> > maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
> > webpage  : http://www.pragma-ade.nl / http://context.aanhet.net
> > archive  : https://bitbucket.org/phg/context-mirror/commits/
> > wiki     : http://contextgarden.net
> > ___________________________________________________________________________________
>
> ___________________________________________________________________________________
> If your question is of interest to others as well, please add an entry to the Wiki!
>
> maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
> webpage  : http://www.pragma-ade.nl / http://context.aanhet.net
> archive  : https://bitbucket.org/phg/context-mirror/commits/
> wiki     : http://contextgarden.net
> ___________________________________________________________________________________
___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!

maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : http://www.pragma-ade.nl / http://context.aanhet.net
archive  : https://bitbucket.org/phg/context-mirror/commits/
wiki     : http://contextgarden.net
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: simplifying repeated macro calls in metafun
  2021-06-21  4:26   ` Aditya Mahajan
@ 2021-06-21 12:07     ` Otared Kavian
  2021-06-21 12:21       ` Mikael Sundqvist
  0 siblings, 1 reply; 15+ messages in thread
From: Otared Kavian @ 2021-06-21 12:07 UTC (permalink / raw)
  To: mailing list for ConTeXt users

Hi Aditya,

Thanks for the detailed file you sent to answer Dalyoung’s question and file. And going through both files I have a question.
I see that both of you use a function « whatever » (but is it a function ?): what does this do and where can it be used (actually I have seen Hans using this expression, but I had always thought this is just a name…

Best regards: Otared

> On 21 Jun 2021, at 06:26, Aditya Mahajan <adityam@umich.edu> wrote:
> 
> On Mon, 21 Jun 2021, Jeong Dal wrote:
> 
>> 
>> Hi,
>> 
>> I defined a macro definition in metafun, and draw figures using \startbuffer[] … \stopbuffer and \processMPbuffer[].
>> As you see in the attached code, the same macro definition is used repeatedly by changing the numeric argument. 
>> Is there a better way to simplify the code, for example, using \dorecurse and recurselevel?
> 
> See attached. The main idea is to store the repetitive bits in \startuseMPgraphic{...} and then reuse them using \includeMPgraphic. 
> 
> Aditya<mp-test.tex>___________________________________________________________________________________
> If your question is of interest to others as well, please add an entry to the Wiki!
> 
> maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
> webpage  : http://www.pragma-ade.nl / http://context.aanhet.net
> archive  : https://bitbucket.org/phg/context-mirror/commits/
> wiki     : http://contextgarden.net
> ___________________________________________________________________________________

___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!

maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : http://www.pragma-ade.nl / http://context.aanhet.net
archive  : https://bitbucket.org/phg/context-mirror/commits/
wiki     : http://contextgarden.net
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: simplifying repeated macro calls in metafun
  2021-06-20 23:30 ` Jeong Dal
@ 2021-06-21  4:26   ` Aditya Mahajan
  2021-06-21 12:07     ` Otared Kavian
  0 siblings, 1 reply; 15+ messages in thread
From: Aditya Mahajan @ 2021-06-21  4:26 UTC (permalink / raw)
  To: mailing list for ConTeXt users

[-- Attachment #1: Type: text/plain, Size: 533 bytes --]

On Mon, 21 Jun 2021, Jeong Dal wrote:

> 
> Hi,
> 
> I defined a macro definition in metafun, and draw figures using \startbuffer[] … \stopbuffer and \processMPbuffer[].
> As you see in the attached code, the same macro definition is used repeatedly by changing the numeric argument. 
> Is there a better way to simplify the code, for example, using \dorecurse and recurselevel?

See attached. The main idea is to store the repetitive bits in \startuseMPgraphic{...} and then reuse them using \includeMPgraphic. 

Aditya

[-- Attachment #2: Type: application/x-tex, Size: 2048 bytes --]

[-- Attachment #3: Type: text/plain, Size: 493 bytes --]

___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!

maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : http://www.pragma-ade.nl / http://context.aanhet.net
archive  : https://bitbucket.org/phg/context-mirror/commits/
wiki     : http://contextgarden.net
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 15+ messages in thread

* simplifying repeated macro calls in metafun
       [not found] <mailman.1.1624183201.7245.ntg-context@ntg.nl>
@ 2021-06-20 23:30 ` Jeong Dal
  2021-06-21  4:26   ` Aditya Mahajan
  2021-06-21 13:34 ` Jeong Dal
  1 sibling, 1 reply; 15+ messages in thread
From: Jeong Dal @ 2021-06-20 23:30 UTC (permalink / raw)
  To: list ntg-context@ntg.nl ntg-context@ntg.nl ntg-context@ntg.nl
	ntg-context@ntg.nl


[-- Attachment #1.1: Type: text/plain, Size: 2980 bytes --]


Hi,

I defined a macro definition in metafun, and draw figures using \startbuffer[] … \stopbuffer and \processMPbuffer[].
As you see in the attached code, the same macro definition is used repeatedly by changing the numeric argument. 
Is there a better way to simplify the code, for example, using \dorecurse and recurselevel?

Thank you for reading.

Best regards,

Dalyoung

\startbuffer[UDBasic]
  pair A,B,C,D;
  pair E, F, G, H;
  path p,q;
  numeric ang,maxNum;
  u:=1in;
  A=(.95*TextWidth,u); B=(0,u); C=(0,0); D=(.95*TextWidth,0);
  p := A--B--C--D;
  q := (TextWidth,1.5u)--(0,1.5u)--(0,-.5u)--(TextWidth,-.5u)--cycle;
  z0=(.1u,0);
  
   vardef nextPt (expr M,N) = 
   if ypart(N) = 0: 
       H := whatever[N, N + dir(.5*angle(M-N))*u] = whatever[A,B];
   else:
       H := whatever[N, N + dir(.5*angle(M-N))*u] = whatever[C,D];
   fi;
   H
   enddef;

   def grayUD(expr i) =
      draw G -- if ypart(B)=ypart(G): B -- C else: C -- B fi --z[i];
      filldraw z[i]+2*(G-z[i])*u -- z[i] -- z[i+1] -- z[i+1]+(G-z[i])*u -- cycle withcolor .75white; 
      draw G -- z[i+1] dashed evenly;
      draw z[0] for k=0 upto i: --z[k] endfor dashed evenly;
      clip currentpicture to q;
   enddef;

   def linesUD(expr i) =
      draw p;
      draw z0 for k=0 upto i+1: --z[k] endfor dashed evenly;
      setbounds currentpicture to boundingbox q;
   enddef;
\stopbuffer

\startbuffer[UDpts]
   ang := 40;
   z[1] = whatever[z[0], z[0]+dir(ang)*u] = whatever[A,B];
   G := whatever[z[0], z[0]+dir(2*ang)*u] = whatever[A,B];
   for k = 2 upto 20:
      z[k] = nextPt(z[k-2],z[k-1]);
      maxNum := k;
      exitif xpart(z[k]) > TextWidth;
   endfor;
\stopbuffer
\startbuffer[UD0]
   draw p;
   setbounds currentpicture to boundingbox q;
\stopbuffer
\startbuffer[UD1]
   grayUD(0);
\stopbuffer
\startbuffer[UD11]
   linesUD(0);
\stopbuffer
\startbuffer[UD2]
   G := z0;
   grayUD(1)
\stopbuffer
\startbuffer[UD21]
   linesUD(1);
\stopbuffer
\startbuffer[UD3]
   G := z1;
   grayUD(2);
\stopbuffer
\startbuffer[UD31]
   linesUD(2);
\stopbuffer
\startbuffer[UD4]
   G := z2;
   grayUD(3);
\stopbuffer
\startbuffer[UD41]
   linesUD(3);
\stopbuffer
\startbuffer[UD5]
   G := z3;
   grayUD(4);
\stopbuffer
\startbuffer[UD51]
   linesUD(4);
\stopbuffer
\startbuffer[UDFinal]
draw p;
draw z0--z1--z2--z3--z4--z5--z6--z7--z8 dashed evenly;
setbounds currentpicture to boundingbox q;
\stopbuffer
\starttext
\processMPbuffer[UDBasic,UDpts,UD0]
\processMPbuffer[UDBasic,UDpts,UD1]
\processMPbuffer[UDBasic,UDpts,UD11]
\processMPbuffer[UDBasic,UDpts,UD2]
\processMPbuffer[UDBasic,UDpts,UD21]
\processMPbuffer[UDBasic,UDpts,UD3]
\processMPbuffer[UDBasic,UDpts,UD31]
\processMPbuffer[UDBasic,UDpts,UD4]
\processMPbuffer[UDBasic,UDpts,UD41]
\processMPbuffer[UDBasic,UDpts,UD5]
\processMPbuffer[UDBasic,UDpts,UD51]
\processMPbuffer[UDBasic,UDpts,UDFinal]
\stoptext






[-- Attachment #1.2: Type: text/html, Size: 19954 bytes --]

[-- Attachment #2: Type: text/plain, Size: 493 bytes --]

___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!

maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : http://www.pragma-ade.nl / http://context.aanhet.net
archive  : https://bitbucket.org/phg/context-mirror/commits/
wiki     : http://contextgarden.net
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 15+ messages in thread

end of thread, other threads:[~2021-06-25 19:14 UTC | newest]

Thread overview: 15+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <mailman.1069.1624285904.1130.ntg-context@ntg.nl>
2021-06-21 21:50 ` simplifying repeated macro calls in metafun Jeong Dal
2021-06-22 13:49   ` Otared Kavian
2021-06-22 16:00     ` Wolfgang Schuster
     [not found] <mailman.47.1624523134.1143.ntg-context@ntg.nl>
2021-06-25 19:14 ` Jeong Dal
     [not found] <mailman.1145.1624377609.1130.ntg-context@ntg.nl>
2021-06-23 20:26 ` Jeong Dal
2021-06-24  3:31   ` Aditya Mahajan
2021-06-24  3:42     ` Aditya Mahajan
2021-06-24  7:16       ` Hans Hagen
     [not found] <mailman.1.1624183201.7245.ntg-context@ntg.nl>
2021-06-20 23:30 ` Jeong Dal
2021-06-21  4:26   ` Aditya Mahajan
2021-06-21 12:07     ` Otared Kavian
2021-06-21 12:21       ` Mikael Sundqvist
2021-06-21 14:31         ` Otared Kavian
2021-06-21 18:35           ` Mikael Sundqvist
2021-06-21 13:34 ` Jeong Dal

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).