[metapost] how to express "struct"

Laurent Méhats laurent.mehats at gmail.com
Sun Mar 12 01:35:02 CET 2017


Le 11/03/2017 à 19:23, Qiong Cai a écrit :
> Hi,
>
> I am trying to define a C-like struct in metapost:
>
>        struct Pixel {
>              numeric x;
>              numeric y;
>              numeric val;
>        };
>
> What's the best way to define such a struct in MetaPost?
>
> Thanks
> Qiong
>
>
> --
> http://tug.org/metapost/

Hello,

I'm not sure at all it's the best way, but here is how you may proceed:

%--%<-- test.mp
vardef declare_Pixel text STR=
   for str=STR:
     expandafter numeric scantokens str.x;
     expandafter numeric scantokens str.y;
     expandafter numeric scantokens str.val;
   endfor
enddef;

declare_Pixel "a", "b[]";

a.x:=1;
a.y:=2;
a.val:=3;
b[0].x:=4;
b[0].y:=5;
b[0].val:=6;

show a.val;
show b[0].val;
end
%--%<-- test.mp

Regards,
-- 
Laurent Méhats



More information about the metapost mailing list