summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/metapost
diff options
context:
space:
mode:
authorKarl Berry <karl@freefriends.org>2023-04-27 20:36:32 +0000
committerKarl Berry <karl@freefriends.org>2023-04-27 20:36:32 +0000
commit8b0819c723fce9394f8af02362db1b621ec75960 (patch)
treeea34f0ccb9c773d707f68580fc10637aac2fcba2 /Master/texmf-dist/metapost
parent28d3a774f6a6dab436024498c8dbfa28c4aa1a93 (diff)
mptrees (27apr23)
git-svn-id: svn://tug.org/texlive/trunk@66952 c570f23f-e606-0410-a88d-b1316a301751
Diffstat (limited to 'Master/texmf-dist/metapost')
-rw-r--r--Master/texmf-dist/metapost/mptrees/mptrees.mp738
1 files changed, 627 insertions, 111 deletions
diff --git a/Master/texmf-dist/metapost/mptrees/mptrees.mp b/Master/texmf-dist/metapost/mptrees/mptrees.mp
index cedcdb1f95c..b832d0f94a4 100644
--- a/Master/texmf-dist/metapost/mptrees/mptrees.mp
+++ b/Master/texmf-dist/metapost/mptrees/mptrees.mp
@@ -2,10 +2,10 @@
%% mptrees.mp %%
%% Probability trees with MetaPost %%
%% o.peault@posteo.net %%
-%% Version 21.11 (November 2021) %%
+%% Version 23.04 (April 2023) %%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
string mptreesversion;
-mptreesversion:="21.11";
+mptreesversion:="23.04";
%
% This work may be distributed and/or modified under the conditions of
% the LaTeX Project Public License, either version 1.3 of this license
@@ -20,6 +20,30 @@ if not known mplib: input latexmp fi;
boolean arbres_latexmp;
arbres_latexmp:=true;
+
+boolean mpt_begtr; % is begintree used ?
+mpt_begtr:=false;
+numeric mpt_passe;
+mpt_passe:=1;
+
+
+% begintree permet de stocker les instructions de la figure dans mpt_tree pour calculs éventuels
+
+def begintree=
+ mpt_begtr:=true;
+ scantokens "def mpt_tree="
+enddef;
+
+let endtree = enddef;
+
+numeric mpt_tmpdirtree;
+
+
+% à la fin de begintree, on exécute deux fois mpt_tree
+extra_endfig:="if mpt_begtr: mpt_tree currentpicture:=nullpicture; mpt_passe:=2; AdjustNodes; mpt_tree; mpt_begtr:=false;mpt_passe:=1 fi;" & extra_endfig;
+%extra_endfig:="if mpt_begtr: mpt_tree drawoptions(withcolor blue); mpt_passe:=2; AdjustNodes; mpt_tree; mpt_begtr:=false;mpt_passe:=1 fi;" & extra_endfig;
+
+
numeric posprob; % position de l'étiquette sur la branche
posprob:=0.6;
@@ -42,17 +66,36 @@ endedgeshift:=0;
numeric brokenlineratio;
brokenlineratio:=0.2;
+numeric linewidth;
+linewidth:=0.5bp;
+
+color linecolor;
+linecolor:=black;
+
pair Orig_arbre[][]; % sommet de l'arbre i,j
Orig_arbre[1][1]:=origin;
+pair mpt_dec_ori[][]; % Décalage avec la fin de la branche précédente
+mpt_dec_ori[1][1]:=(0,0);
+
+pair mpt_tmp_ori[][]; % Branche i,j,k
+
+pair mpt_br[][][]; % Branche i,j,k
+
+boolean exist.arbre[][]; % L'arbre i,j est-il dessiné ?
+exist.arbre[0][1]:=true;
+
+
numeric colonne_cour,ligne_cour;
+numeric mptLargeur[][],mptHauteur[][]; % diff largeur au rang i, diff hauteur rang i
numeric countligne[]; % nombre de ligne pour le niveau i
+for i=0 upto 128: countligne[i]:=0; endfor
-numeric typedec; % 0 origine dépend de la taille de l'évènement, 1 taille fixée
+%numeric typedec; % 0 origine dépend de la taille de l'évènement, 1 taille fixée
numeric shiftev; % taille du décalage dans le cas où typedec=1
-shiftev:=0; % dans ce cas, typedec =0
-extra_beginfig:=extra_beginfig & "shiftev:=0;"; % remise à zéro en début de figure
+shiftev:=-1; % dans ce cas, typedec =0
+%extra_beginfig:=extra_beginfig & "shiftev:=-1;"; % remise à zéro en début de figure
numeric typeprob; % affichage de la proba sur la branche
@@ -72,18 +115,24 @@ numeric endlabelspace;
endlabelspace:=1cm;
+% Pour rendre les paramètres "locaux" à chaque figure
+extra_beginfig:=extra_beginfig & "init_loc_var;";
+
+
+
% Étiquette au début de l'arbre
picture mptreestartlabel;
mptreestartlabel:=nullpicture;
vardef startlabel(expr s)=
+ colonne_cour:=0;
mptreestartlabel:=labelarbres(s);
if dirtree<>0: dirlabel:=dirtree fi;
if dirlabel>0:
- theevlabel(labelarbres(s) scaled scaleev,origin,-180+dirlabel)
+ theevlabel(formatnodeev(labelarbres(s)) scaled scaleev,origin,-180+dirlabel)
else:
- theevlabel(labelarbres(s) scaled scaleev,origin,180+dirlabel)
+ theevlabel(formatnodeev(labelarbres(s)) scaled scaleev,origin,180+dirlabel)
fi
enddef;
@@ -131,14 +180,161 @@ vardef longdir(expr p,inc)=
fi
enddef;
+%%%%%%%%%%%%%%
+%% format nodes, leaves and probabilities
+%%%%%%%%%%%%%%
+
+% Colors
+color nodelinecolor,nodebgcolor,nodefgcolor;
+nodelinecolor:=black;
+nodebgcolor:=white;
+nodefgcolor:=black;
+
+color leavelinecolor,leavebgcolor,leavefgcolor;
+leavelinecolor:=black;
+leavebgcolor:=white;
+leavefgcolor:=black;
+
+color problinecolor,probbgcolor,probfgcolor;
+problinecolor:=black;
+probbgcolor:=white;
+probfgcolor:=black;
+
+
+% Superellipse
+numeric superellipseparam;
+superellipseparam:=0.85;
+
+vardef superellipsebox(expr p)=
+ save a,b,c,d,l;
+ pair a,b,c,d;
+ path l;
+ l := bbox p;
+ a=0.5[lrcorner l,urcorner l];
+ b=0.5[urcorner l,ulcorner l];
+ c=0.5[ulcorner l,llcorner l];
+ d=0.5[llcorner l,lrcorner l];
+ l:=superellipse(a,b,c,d,superellipseparam);
+ l
+enddef;
+
+vardef supellformat(expr p,cl,cb,cf)=
+image(%
+ fill superellipsebox(p) withcolor cb;
+ draw superellipsebox(p) withcolor cl;
+ draw p withcolor cf;
+)
+enddef;
+
+% Circle
+vardef circlebox(expr p)=
+ save a,b,c;
+ pair a,b,c;
+ a:=urcorner bbox p;
+ b:=llcorner bbox p;
+ c:=0.5[a,b];
+ fullcircle scaled (abs(b-a)-2) shifted c
+enddef;
+
+vardef circleformat(expr p,cl,cb,cf)=
+image(%
+ fill circlebox(p) withcolor cb;
+ draw circlebox(p) withcolor cl;
+ draw p withcolor cf;
+)
+enddef;
+
+% bbox
+vardef bboxformat(expr p,cl,cb,cf)=
+image(%
+ fill bbox p withcolor cb;
+ draw bbox p withcolor cl;
+ draw p withcolor cf;
+)
+enddef;
+
+
+% Format nodes
+
+string nodeformat,leaveformat,probformat; %How nodes and leaves should be printed
+nodeformat:="";
+leaveformat:="";
+probformat:="";
+
+
+
+
+vardef formatprob(expr p)=
+ save cl,cb,cf;
+ color cl,cb,cf;
+ cl:=problinecolor;
+ cb:=probbgcolor;
+ cf:=probfgcolor;
+ if probformat="superellipse":
+ supellformat(p,cl,cb,cf)
+ elseif probformat="circle":
+ circleformat(p,cl,cb,cf)
+ elseif probformat="bbox":
+ bboxformat(p,cl,cb,cf)
+ else:
+ image (draw p withcolor cf)
+ fi
+enddef;
+
+vardef formatnodeev(expr p)=
+ save cl,cb,cf;
+ color cl,cb,cf;
+ cl:=nodelinecolor;
+ cb:=nodebgcolor;
+ cf:=nodefgcolor;
+ if nodeformat="superellipse":
+ supellformat(p,cl,cb,cf)
+ elseif nodeformat="circle":
+ circleformat(p,cl,cb,cf)
+ elseif nodeformat="bbox":
+ bboxformat(p,cl,cb,cf)
+ else:
+ image (draw p withcolor cf)
+ fi
+enddef;
+
+vardef formatnodeleave(expr p)=
+ save cl,cb,cf;
+ color cl,cb,cf;
+ cl:=leavelinecolor;
+ cb:=leavebgcolor;
+ cf:=leavefgcolor;
+ if leaveformat="superellipse":
+ supellformat(p,cl,cb,cf)
+ elseif leaveformat="circle":
+ circleformat(p,cl,cb,cf)
+ elseif leaveformat="bbox":
+ bboxformat(p,cl,cb,cf)
+ elseif leaveformat="none":
+ image (draw p withcolor cf)
+ else:
+ formatnodeev(p)
+ fi
+enddef;
+
+
+vardef formatnode(expr p)=
+ if known exist.arbre[colonne_cour+1][counttmp]:
+ formatnodeev(p)
+ else:
+ formatnodeleave(p)
+ fi
+enddef;
+
+
+%%% Prints events
+
vardef theevlabel(expr s,z,inc)=
- save p; picture p;
save d; numeric d;
- if picture s: p=s
- else: p = textext(s)
- fi;
- d=0.5*longdir(p,inc);
- p shifted (z - center p + (d+labeloffset)*dir(inc))
+% save p;picture p;
+% p:=formatnode(s);
+ d=0.5*longdir(s,inc);
+ s shifted (z - center s + (d+labeloffset)*dir(inc))
enddef;
def evlabel = draw theevlabel enddef;
@@ -225,12 +421,24 @@ vardef brokenline(expr Ori,Fin)=
Ori -- (Ori + M) -- (Fin - M) -- Fin
enddef;
+numeric tenscurve;
+tenscurve:=0;
+
+vardef curveline(expr Ori,Fin)=
+ save tmp_angle;
+ numeric tmp_angle;
+ tmp_angle:=dirlabel+tenscurve*angle((Fin-Ori) rotated -dirlabel);
+ Ori{dir tmp_angle}..{dir tmp_angle}Fin
+enddef;
+
+
+
%%%%%%%% Dessin de la branche
vardef dessinbranche(expr Ori,Fin)=
if branchtype="segment":
Ori--Fin
elseif branchtype="curve":
- Ori{dir dirlabel}..{dir dirlabel}Fin
+ curveline(Ori,Fin)
elseif branchtype="broken":
brokenline(Ori,Fin)
else:
@@ -239,92 +447,113 @@ vardef dessinbranche(expr Ori,Fin)=
enddef;
-%%%%%%%% Dessin complet de la branche (avec proba, évènement
+%%%%%%%% Dessin complet de la branche (avec proba, évènement)
vardef branche_abs(expr Ori,Fin,Eve,Pro)=
save figtmp,thelab,figlab,tmpshft;
picture figtmp,thelab,figlab;
numeric tmpshft;
thelab:=labelarbres(Pro) scaled scaleprob;
- if ypart Ori > ypart Fin: %le décalage se fait en fonction de la direction de la branche
+ if ypart (Ori rotated -dirtree) > ypart (Fin rotated -dirtree): %le décalage se fait en fonction de la direction de la branche
tmpshft:=endedgeshift
else:
tmpshft:=-endedgeshift
fi;
- figlab:=thelabelbranche(Ori,(Fin+ (0,tmpshft)) rotatedaround(Ori,dirtree),thelab);
+ figlab:=thelabelbranche(Ori,(Fin+ (0,tmpshft)),thelab);
figtmp=image(%
if branchtype<>"none":
- if edgearrow:
+ if edgearrow:
drawarrow
else:
draw
fi
-% dessinbranche(Ori,Fin + (0,tmpshft)) rotatedaround(Ori,dirtree)
- dessinbranche(Ori,(Fin + (0,tmpshft)) rotatedaround(Ori,dirtree))
- fi;
+ dessinbranche(Ori,(Fin + (0,tmpshft))) withpen pencircle scaled linewidth withcolor linecolor;
+ fi
draw figlab;
- evlabel(Eve,Fin rotatedaround(Ori,dirtree),dirlabel)
+ evlabel(Eve,Fin,dirlabel)
);
figtmp
enddef;
+
vardef branche_rel(expr Ori,dx,dy,Eve,Pro)=
branche_abs(Ori,Ori+(dx,dy),Eve,Pro)
enddef;
+pair mptFinBranche[][]; % Branche qui mène au noeud [][]
+mptFinBranche[1][1]:=origin;
+
+pair mptFinBrancheRel[][][]; % k-ieme branche qui part de i,j
+mptFinBrancheRel[0][1][1]:=origin;
+
+numeric mptLargBranche[][], mptHautBranche[][]; % Branche qui mène au noeud [][]
+numeric mptNbBranches[][]; % Nombre de branches de l'arbre [][]
+for i=0 upto 10: for j=0 upto 100: mptNbBranches[i][j]:=1; endfor endfor
+mptNbBranches[0][1]:=1;
+
+
+numeric mptAbs[]; % Abscisse noeud i
+mptAbs[1]:=0;
+
+pair mptchild[][][];
+mptchild[0][1][1]:=(1,1);
+
+color mptparent[][];
+mptparent[1][1]:=(0,1,1);
+
vardef _arbre_(expr Ori)(text t)(text r)= % (pt)(pt1,pt2...)(ev,proba,ev,proba...)
- save figtmp,n,Fintmp,labeltmp,counttmp,countlab;
+ save figtmp,n,Fintmp,labeltmp,counttmp,countlab,tmpcountligne;
picture figtmp,labeltmp[];
- numeric counttmp,countlab;
+ numeric counttmp,countlab,tmpcountligne;
pair Fintmp[];
if dirtree<>0: dirlabel:=dirtree fi;
- if colonne_cour=1: for i=1 upto 128: countligne[i]:=0; endfor fi
+ if colonne_cour=1:
+ countligne[1]:=1;countligne[0]:=1;
+ for i=2 upto 128: countligne[i]:=0; endfor
+ fi
+ tmpcountligne:=countligne[colonne_cour+1];
+ counttmp:=tmpcountligne;
+ for i=t:
+ counttmp:=counttmp+1;
+ countligne[colonne_cour+1]:=countligne[colonne_cour+1]+1;
+ mptFinBrancheRel[colonne_cour][ligne_cour][counttmp-tmpcountligne]:=i rotatedaround(Ori,dirtree);
+ mptFinBranche[colonne_cour+1][counttmp]:=i rotatedaround(Ori,dirtree);
+ mptLargBranche[colonne_cour+1][counttmp]:= xpart i - xpart Ori;
+ mptHautBranche[colonne_cour+1][counttmp]:= ypart i - ypart Ori;
+ mptchild[colonne_cour][ligne_cour][counttmp-tmpcountligne]:=(colonne_cour+1,counttmp);
+ mptparent[colonne_cour+1][counttmp]:=(colonne_cour,ligne_cour,counttmp-tmpcountligne);
+ endfor;
+ mptAbs[colonne_cour+1]:= xpart mptFinBranche[colonne_cour+1][counttmp];
+ mptNbBranches[colonne_cour][ligne_cour]:=counttmp-tmpcountligne;
figtmp=image(%
- counttmp:=0;
- for i=t:
- counttmp:=counttmp+1;
- Fintmp[counttmp]:=i;
- endfor;
- counttmp:=0;countlab:=0;
- for i=r:
- countlab:=countlab+1;
- labeltmp[countlab]:=labelarbres(i);
- if countlab=2:
- counttmp:=counttmp+1;countligne[colonne_cour+1]:=countligne[colonne_cour+1]+1;
- draw branche_abs(Ori,Fintmp[counttmp],labeltmp[1] scaled scaleev,labeltmp[2]);
- Orig_arbre[colonne_cour+1][countligne[colonne_cour+1]]:=Fintmp[counttmp] shifted
- if typedec=0: ((longdir(labeltmp[1] scaled scaleev,dirlabel)+2labeloffset)*(1,0))
+ counttmp:=tmpcountligne;countlab:=0;
+ for i=r:
+ countlab:=countlab+1;
+ labeltmp[countlab]:=labelarbres(i);
+ if countlab=2:
+ counttmp:=counttmp+1;
+ labeltmp[1]:=formatnode(labeltmp[1]);
+ labeltmp[2]:=formatprob(labeltmp[2]);
+ draw branche_abs(Ori,mptFinBranche[colonne_cour+1][counttmp],labeltmp[1] scaled scaleev,labeltmp[2]);
+ Orig_arbre[colonne_cour+1][counttmp]:=mptFinBranche[colonne_cour+1][counttmp] shifted
+ if shiftev<0: ((longdir(labeltmp[1] scaled scaleev,dirlabel) + 2labeloffset)*(1,0))
else: (shiftev,0)
fi
- if dirtree=0: rotatedaround(Fintmp[counttmp],dirlabel) fi
- rotatedaround(Ori,dirtree);
- countlab:=0
+ rotatedaround(mptFinBranche[colonne_cour+1][counttmp],dirlabel);
+ if mpt_passe=1:
+ mpt_dec_ori[colonne_cour+1][counttmp]:= (Orig_arbre[colonne_cour+1][counttmp] rotatedaround(Ori,-dirtree))- mptFinBranche[colonne_cour+1][counttmp]
fi;
- endfor);
+ countlab:=0
+ fi;
+ endfor
+ );
figtmp
enddef;
-vardef arbre_var(expr Ori,horiz,Ymax)(text t)(text r)= % (pt,dec horiz,Ymax)(dy1,dy2...)(ev,proba,ev,proba...)
- save Fin,compt,Ytmp;
- numeric compt,Ytmp;
- pair Fin[];
- Fin[0]:=Ori+(horiz,Ymax);
- compt:=0;Ytmp:=Ymax;
- for i=t:
- compt:=compt+1;Ytmp:=Ytmp-i;
- Fin[compt]:=Ori+(horiz,Ytmp);
- endfor
- def ptarr=
- Fin[0]
- for i=1 upto compt:
- ,Fin[i]
- endfor
- enddef;
- _arbre_(Ori)(ptarr)(r)
-enddef;
+
vardef arbre_decv(expr Ori,horiz,Ymax)(text t)(text r)=
@@ -379,7 +608,7 @@ vardef arbre_fixe(expr Ori,horiz,Ymax,decy)(text r)=
endfor
def ptarr=
Fin[0]
- for i=1 upto compt:
+ for i=1 upto compt/2-1:
,Fin[i]
endfor
enddef;
@@ -399,22 +628,24 @@ vardef colonne.dec.arbre[]=
@
enddef;
+vardef colonne.calc.arbre[]=
+ @
+enddef;
+
+vardef colonne.arbre[]=
+ @
+enddef;
vardef reg.arbre[][](text t)(text r)=
save compt,decal;
numeric compt,decal[];
colonne_cour:=colonne.#@;
ligne_cour:=@;
- typedec:=0;
- if shiftev<>0: typedec:=1 fi;
compt:=0;
for i=t:
if numeric i:
compt:=compt+1;
decal[compt]:=i
- else:
- typedec:=1;
- scantokens i
fi;
endfor
compt:=0;
@@ -429,17 +660,9 @@ enddef;
vardef comp.arbre[][](text t)(text r)=
colonne_cour:=colonne.#@;
ligne_cour:=@;
- typedec:=0;
- if shiftev<>0: typedec:=1 fi;
save tmp_compt;
numeric tmp_compt;
tmp_compt:=0;
- for i=t:
- if string i:
- typedec:=1;
- scantokens i
- fi;
- endfor
if abscoord:
def tmp_text=t enddef
else:
@@ -464,8 +687,6 @@ vardef dec.arbre[][](text t)(text r)=
numeric decal[];
colonne_cour:=colonne.#@;
ligne_cour:=@;
- typedec:=0;
- if shiftev<>0: typedec:=1 fi;
save tmp_compt;
numeric tmp_compt;
tmp_compt:=0;
@@ -473,9 +694,6 @@ vardef dec.arbre[][](text t)(text r)=
if numeric i:
tmp_compt:=tmp_compt+1;
decal[tmp_compt]:=i
- else:
- typedec:=1;
- scantokens i
fi;
endfor
def tmp_text=
@@ -486,29 +704,298 @@ vardef dec.arbre[][](text t)(text r)=
enddef;
arbre_decv(Orig_arbre[colonne_cour][ligne_cour],decal[1],decal[2])(tmp_text)(r)
enddef;
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%5
+
+
+vardef calc.arbre[][](text r)=
+ colonne_cour:=colonne.#@;
+ ligne_cour:=@;
+ save mpt_O,mpt_dec_calc;
+ pair mpt_O;
+ numeric mpt_dec_calc[];
+ for i=1 upto mptNbBranches[colonne_cour][ligne_cour]:
+ mpt_dec_calc[i]:=mpt_decbranche[colonne_cour][ligne_cour][i];
+ endfor
+ mpt_dec_calc[0]:=widthbranch*(scalebranch**colonne_cour);
+ def tmp_text_calc=
+ mpt_dec_calc[0]
+ for i=1 upto mptNbBranches[colonne_cour][ligne_cour]:
+ ,mpt_dec_calc[i]
+ endfor
+ enddef;
+ mpt_O:= mpt_tmp_ori[colonne_cour][ligne_cour];
+ dec.arbre[colonne_cour][ligne_cour](tmp_text_calc)(r)
+% _arbre_(mpt_O)(tmp_text_calc)(r)
+enddef;
+
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+% Macro principale - Modification Mars 2022 (variables locales)
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+
+
+% Conversion des ' en " pour les variables locales.
+
+vardef singletodoublequote(expr chdb)=
+ save chsi;
+ string chsi;
+ chsi:="";
+ for i=1 upto length(chdb):
+ if ASCII(substring(i-1,i) of chdb) = 39:
+ chsi := chsi & char(34)
+ else:
+ chsi := chsi & substring(i-1,i) of chdb
+ fi;
+ endfor
+ chsi
+enddef;
+
+% Initialisation variables locales
+
+def init_loc_var=
+ numeric tmp_posprob, tmp_shiftev, tmp_typeprob, tmp_scaleev, tmp_scaleprob, tmp_proboffset, tmp_endedgeshift;
+ boolean tmp_edgearrow;
+ string tmp_branchtype;
+ tmp_posprob := posprob;
+ tmp_typeprob := typeprob;
+ tmp_shiftev:= shiftev;
+ tmp_scaleev := scaleev;
+ tmp_scaleprob := scaleprob;
+ tmp_proboffset := proboffset;
+ tmp_endedgeshift := endedgeshift;
+ tmp_edgearrow := edgearrow;
+ tmp_branchtype := branchtype;
+ save posprob, shiftev, typeprob, scaleev, scaleprob, proboffset, endedgeshift, edgearrow, branchtype;
+ numeric posprob, shiftev, typeprob, scaleev, scaleprob, proboffset, endedgeshift;
+ boolean edgearrow;
+ string branchtype;
+ posprob:=tmp_posprob;
+ typeprob:=tmp_typeprob;
+ shiftev:=tmp_shiftev;
+ scaleev := tmp_scaleev;
+ scaleprob := tmp_scaleprob;
+ proboffset := tmp_proboffset;
+ endedgeshift := tmp_endedgeshift;
+ edgearrow := tmp_edgearrow;
+ branchtype := tmp_branchtype;
+enddef;
+
+
+pair current_tree;
vardef arbre[][](text t)(text r)=
- save tmp_type,tmp_compt;
- numeric tmp_type,tmp_compt;
- tmp_compt:=0;
+ init_loc_var;
+ save tmp_type,tmp_compt,tmp_chdb;
+ numeric tmp_type,tmp_compt,tmp_comptii;
+ string tmp_chdb;
+ tmp_compt:=0;tmp_comptii:=0;
for i=t:
- if numeric i: tmp_compt:=tmp_compt+1 fi;
+ tmp_comptii:=tmp_comptii+1;
+ if numeric i:
+ tmp_compt:=tmp_compt+1
+ elseif string i:
+ tmp_chdb:=singletodoublequote(i);
+ scantokens tmp_chdb;
+ fi;
endfor
- if tmp_compt=0: % Arbre défini par des points (dec_horiz,dec_vert)
- comp.#@.@(t)(r)
- elseif tmp_compt=2: % Arbre régulier, on donne largeur et esp. vert.
- reg.#@.@(t)(r)
+ exist.#@.@:=true;
+ current_tree:=(colonne.#@,@);
+ if tmp_comptii=0:
+ if mpt_passe=1:
+ reg.#@.@(widthbranch*(scalebranch**colonne_cour),gapnode*(2**(3-colonne_cour)))(r)
+ else:
+ calc.#@.@(r)
+ fi
else:
- dec.#@.@(t)(r) % Arbre semi-régulier, largeur fixe, decalages vert.
+ Orig_arbre[1][1]:=origin;
+ if tmp_compt=0: % Arbre défini par des points (dec_horiz,dec_vert)
+ comp.#@.@(t)(r)
+ elseif tmp_compt=2: % Arbre régulier, on donne largeur et esp. vert.
+ reg.#@.@(t)(r)
+ else:
+ dec.#@.@(t)(r) % Arbre semi-régulier, largeur fixe, decalages vert.
+ fi
fi
enddef;
+
def tree = arbre enddef;
+%%%%% stree (simple tree) Arbre sans les probas
+
+vardef colonne.stree[]=
+ @
+enddef;
+
+vardef stree[][](text t)(text r)=
+ save cc,ll;
+ numeric cc,ll;
+ cc:=colonne.#@;
+ ll:=@;
+ def tmptxt=
+ for i=r:
+ i,"",
+ endfor
+ enddef;
+ tree[cc][ll](t)(tmptxt)
+enddef;
+
+
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%%%%%%%% "Calculated" trees
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+
+vardef ymean(expr i,j)=
+ (0
+ for k=1 upto mptNbBranches[i][j]:
+ + ypart (mptFinBrancheRel[i][j][k] rotated -dirtree)
+ endfor)/mptNbBranches[i][j]
+enddef;
+
+
+vardef barycentre(expr A,a,B,b)=
+ (a*A+b*B)/(a+b)
+enddef;
+
+numeric mpt_fact_gap;
+mpt_fact_gap:=1.5;
+
+vardef exlignes(expr col,lig)=
+% renvoie (x,y) tel que x<lig<y, x et y ont des child, 0 sinon
+ save tmpcl,tmpx,tmpy,ytrue;
+ pair tmpcl;
+ numeric tmpx,tmpy;
+ boolean ytrue;
+ tmpx:=0;tmpy:=0;ytrue:=false;
+ for i=1 upto countligne[col+1]:
+ if known exist.arbre[col+1][i]:
+ if i<lig:
+ tmpx:=i
+ else:
+ tmpy:=i;ytrue:=true;
+ fi
+ fi;
+ exitif ytrue;
+ endfor
+ (tmpx,tmpy)
+enddef;
+
+
+vardef mptCalcIsol(expr a,b,c,l)=
+ save tmpmin,tmpmax;
+ numeric tmpmin,tmpmax;
+ tmpmin:=xpart exlignes(a,l);
+ tmpmax:=ypart exlignes(a,l);
+ if tmpmin=0:
+ mptFinBrancheRel[a][greenpart mptparent[a+1][tmpmax]][bluepart mptparent[a+1][tmpmax]] + (tmpmax-l)*gapnode*mpt_fact_gap*((0,1) rotated dirtree)
+ elseif tmpmax=0:
+ mptFinBrancheRel[a][greenpart mptparent[a+1][tmpmin]][bluepart mptparent[a+1][tmpmin]] - (l-tmpmin)*gapnode*mpt_fact_gap*((0,1) rotated dirtree)
+ else:
+ barycentre(mptFinBrancheRel[a][greenpart mptparent[a+1][tmpmin]][bluepart mptparent[a+1][tmpmin]],tmpmax-l,mptFinBrancheRel[a][greenpart mptparent[a+1][tmpmax]][bluepart mptparent[a+1][tmpmax]],l-tmpmin)
+ fi
+enddef;
+
+
+numeric tmplig,tmpligii;
+
+numeric mpt_decbranche[][][];
+
+
+
+def AdjustNodes=
+ tmplig:=0;
+ for i=1 upto countligne[colonne_cour]:
+ for j= 1 upto mptNbBranches[colonne_cour][i]:
+ mptFinBrancheRel[colonne_cour][i][j]:=mptFinBranche[colonne_cour+1][1] -tmplig*gapnode*((0,1) rotated dirtree);
+ tmplig:=tmplig+1;
+ endfor
+ endfor
+ for cmpttmp:=1 upto colonne_cour-1: % plusieurs tours pour tout régler...
+ for col=colonne_cour-1 downto 0:
+ tmplig:=0;
+ for i=1 upto countligne[col]:
+ if known mptNbBranches[col][i]:
+ for j= 1 upto mptNbBranches[col][i]:
+ if known exist.arbre[col][i]:
+ tmplig:=tmplig+1;
+ if known exist.arbre[col+1][tmplig]:
+ mptFinBrancheRel[col][i][j]:=
+ (xpart (mptFinBrancheRel[col][i][j] rotated -dirtree),ymean(col+1,tmplig)) rotated dirtree;
+ else:
+ mptFinBrancheRel[col][i][j]:=mptCalcIsol(col,i,j,tmplig);
+ fi;
+ Orig_arbre[col+1][tmplig]:=(xpart (Orig_arbre[col+1][tmplig] rotated -dirtree),ypart (mptFinBrancheRel[col][i][j] rotated -dirtree)) rotated dirtree;
+ fi;
+ endfor
+ fi
+ endfor
+ endfor
+% adjustii;
+ endfor
+ for col=1 upto colonne_cour:
+ for i=1 upto countligne[col]:
+ if known exist.arbre[col][i]:
+ for j=1 upto mptNbBranches[col][i]:
+ mpt_decbranche[col][i][j]:=ypart (mptFinBrancheRel[col][i][j] rotated -dirtree) - ypart (Orig_arbre[col][i] rotated -dirtree);
+ endfor
+ fi
+ endfor
+ endfor
+ Orig_arbre[1][1]:=origin;
+enddef;
+
+
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%%%%%%%% regular trees
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+
+numeric scalebranch;
+scalebranch := 0.8;
+
+numeric widthbranch,gapnode;
+widthbranch:=3.5cm;
+gapnode:=0.7cm;
+
+vardef regulartree(expr n)(text t)(text r)=
+ save tmp,ll,shiftev,tmpstr,count,cc,tmp_compt,h;
+ numeric ll,count,cc,tmp_compt,h;
+ string tmpstr[][];
+ count:=0;cc:=1;
+ for i = r:
+ count := count + cc;
+ cc := 1 - cc;
+ tmpstr[count][cc] := i;
+ endfor
+ shiftev:=-1;
+ for j=1 upto count:
+ if shiftev < longdir(labelarbres(tmpstr[j][0]),dirtree):
+ shiftev := longdir(labelarbres(tmpstr[j][0]),dirtree)
+ fi;
+ endfor
+ shiftev := shiftev + 2*labeloffset;
+ tmp_compt:=1;
+ for i=t:
+ if (not string i) and (tmp_compt=1):
+ ll := i; tmp_compt:=2
+ elseif (not string i) and (tmp_compt=2):
+ h := i
+ else:
+ scantokens i
+ fi;
+ endfor
+ picture tmp;
+ tmp := image(%
+ for i := 1 upto n:
+ for j := 1 upto (count**(i-1)):
+ draw tree[i][j](ll,h*(count**(n-i)))(r);
+ endfor
+ ll := ll * scalebranch;
+ endfor
+ );
+ tmp
+enddef;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%% Binomial
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
string bernoullisuccessevent, bernoullifailureevent, bernoullisuccessprob, bernoullifailureprob;
bernoullisuccessevent := "$S$";
@@ -518,36 +1005,65 @@ bernoullifailureprob := "$q$";
numeric bernoulliscalebranch;
bernoulliscalebranch := 0.8;
-vardef bernoulliprocess(expr n)(expr l,h)=
- save tmp,ll,shiftev;
- numeric ll;
- shiftev:=max(longdir(labelarbres(bernoullisuccessevent),dirtree),longdir(labelarbres(bernoullifailureevent),dirtree))+2*labeloffset;
- ll := l;
- picture tmp;
- tmp := image(%
- for i := 1 upto n:
- for j := 1 upto (2**(i-1)):
- draw tree[i][j](ll,h*(2**(n-i)))(bernoullisuccessevent, bernoullisuccessprob, bernoullifailureevent, bernoullifailureprob);
- endfor
- ll := ll * bernoulliscalebranch;
+vardef bernoulliprocess(expr n)(text t)(text r)=
+ save tmp_ct,tmp_ch;
+ numeric tmp_ct;
+ string tmp_ch[];
+ tmp_ch[1]:=bernoullisuccessevent;
+ tmp_ch[2]:=bernoullisuccessprob;
+ tmp_ch[3]:=bernoullifailureevent;
+ tmp_ch[4]:=bernoullifailureprob;
+ tmp_ct:=0;
+ for i=r:
+ tmp_ct:=tmp_ct+1;
+ if string i:
+ tmp_ch[tmp_ct]:=i
+ fi;
endfor
- );
- tmp
+ regulartree(n)(t)(tmp_ch[1],tmp_ch[2],tmp_ch[3],tmp_ch[4])
enddef;
-vardef bernoulliprocessL(expr n)(expr L,H)=
- save l,h,lpic,hpic;
- numeric l,h,lpic,hpic;
- hpic:=0.5*longdir(labelarbres(bernoullisuccessevent),90+dirtree) + 0.5*longdir(labelarbres(bernoullifailureevent),90+dirtree);
- h := (H-hpic)/((2**n)-1);
- lpic:=longdir(mptreestartlabel,dirtree) + n*max(longdir(labelarbres(bernoullisuccessevent),dirtree) , longdir(labelarbres(bernoullifailureevent),dirtree)) + (2*n-1)*labeloffset;
+
+vardef bernoulliprocessL(expr n)(text t)(text r)=
+ save L,H,l,hh,lpic,hpic;
+ numeric L,H,l,hh,lpic,hpic;
+ save tmp_ct,tmp_chi,tmp_b;
+ numeric tmp_ct;
+ string tmp_chi[];
+ boolean tmp_b;
+ tmp_chi[1]:=bernoullisuccessevent;
+ tmp_chi[2]:=bernoullisuccessprob;
+ tmp_chi[3]:=bernoullifailureevent;
+ tmp_chi[4]:=bernoullifailureprob;
+ tmp_b:=true;
+ for i=t:
+ if numeric i:
+ if tmp_b: L:=i; tmp_b := false else: H:=i fi
+ fi;
+ endfor
+ tmp_ct:=0;
+ for i=r:
+ tmp_ct:=tmp_ct+1;
+ if string i:
+ tmp_chi[tmp_ct]:=i
+ fi;
+ endfor
+ hpic:=0.5*longdir(labelarbres(tmp_chi[1]),90+dirtree) + 0.5*longdir(labelarbres(tmp_chi[3]),90+dirtree);
+ hh := (H-hpic)/((2**n)-1);
+ lpic:=longdir(mptreestartlabel,dirtree) + n*max(longdir(labelarbres(tmp_chi[1]),dirtree) , longdir(labelarbres(tmp_chi[3]),dirtree)) + (2*n-1)*labeloffset;
if bernoulliscalebranch<>1:
l:= (L-lpic)*(1-bernoulliscalebranch)/(1-bernoulliscalebranch**n)
else:
l:= (L-lpic)/n
fi;
- bernoulliprocess(n)(l,h)
+ def tmp_text=
+ l,hh
+ for i=t:
+ if string i: ,i fi
+ endfor
+ enddef;
+ regulartree(n)(tmp_text)(tmp_chi[1],tmp_chi[2],tmp_chi[3],tmp_chi[4])
enddef;