[metapost] Redefine ..
Laurent Méhats
laurent.mehats at gmail.com
Wed Jun 6 18:11:04 CEST 2012
Le 04/06/2012 18:00, Troy Henderson a écrit :
> I am drawing 3D graphics, and many of my "points" are colors instead of
> pairs. Of course, the colors are projected to pairs eventually, but I
> would like to define paths of colors directly with .. by doing something like
>
> path p;
> p:=(1,2,3)..(4,5,6)..(7,8,9);
>
> I have a vardef P that transforms colors into pairs, and thus I can always
> just do
>
> p:=P(1,2,3)..P(4,5,6)..P(7,8,9);
>
> but I would like to be able to omit this P usage. My attempt thusfar is
>
> ---
> def pathnode primary a =
> if color a:
> P(a)
> else:
> a
> fi;
> enddef;
>
> primarydef a .. b =
> pathnode(a) .. pathnode(b)
> enddef;
> ---
>
> but this is not working. I would appreciate any insight into how I might
> fix this so that I can use .. between pairs.
>
> Thanks in advance,
>
> Troy Henderson
>
>
> --
> http://tug.org/metapost/
A dirty workaround could be to scan color-path expressions looking for
colors, then prefixing them with P (thus turning
"(1,2,3)..(4,5,6)..(7,8,9)" into "P(1,2,3)..P(4,5,6)..P(7,8,9)"). Here is
a naive attempt.
vardef P expr clr=
(redpart clr, greenpart clr)
enddef;
vardef map @# expr pth=
string fun, aux, res, chr, acc;
numeric len;
fun:=str @#;
aux:=pth;
len:=length aux;
res:="";
forever:
exitif len=0;
chr:=substring(0, 1) of aux;
aux:=substring(1, len) of aux;
len:=len-1;
if chr<>"(":
res:=res&chr;
else: % we look for the next ")" occurrence
acc:=chr;
forever:
exitif len=0;
chr:=substring(0, 1) of aux;
aux:=substring(1, len) of aux;
len:=len-1;
acc:=acc&chr;
exitif chr=")";
endfor
if color scantokens acc:
res:=res&fun&acc;
else:
res:=res&acc;
fi
fi
endfor
show res;
scantokens res
enddef;
draw map P "(0, 1, 2) .. controls (3, 4, 5) .. {(6, 7, 8)} (9, 0) .. cycle";
>> "P(0, 1, 2) .. controls P(3, 4, 5) .. {P(6, 7, 8)} (9, 0) .. cycle"
Regards,
Laurent Méhats
More information about the metapost
mailing list