Extensions MetaFun qui facilitent la vie

Les commandes étudiées dans cette page sont des macros MetaFun et ne font pas partie du langage MetaPost

Dans cette fiche je me contente de reprendre les exemples du manuel de Metafun. Dans ces exemples plusieurs commandes reviennent souvent. Il s'agit de commandes MetaFun, drawpath qui dessine le path avec un trait épais gris et drawpoints qui dessine les points utilisés dans la construction du path et drawpointlabels qui numérote les points de construction d'un chemin. Ces commandes permettent de mieux visualiser les actions des extensions.

Deux autres commandes permettent de modifier l'aspect des chemins pour l'une drawpathoptions et des points pour l'autre drawpointoptions pour tous les dessins qui les suivent. Ces deux commandes fonctionnent avec resetdrawoptions.

resetdrawoptions est obligatoire si tu ne le mets pas tu n'obtient pas le résultat escompté.

Après resetdrawoptions les dessins reprennent les valeurs par défaut.

\starttext
\startMPcode
path p,q,r,s;
p:=(0,0) -- (1cm,0cm) -- (2cm,0);
q:=(1cm,1cm)..(.5cm,2cm)..(1cm,3cm);
r:= fullcircle scaled 2cm shifted (4cm,0);
s:=(4cm,2cm) -- (5cm,2cm) -- (6cm,2cm);
drawpath p ;
drawpoints p;
drawpointlabels p;
drawpath q ;
drawpoints q;
drawpointlabels q;
drawpathoptions (withpen pencircle scaled 5pt withcolor .625yellow) ;
drawpointoptions(withpen pencircle scaled 4pt withcolor .625red) ;
drawpath r ;
drawpoints r;
drawpointlabels r;
drawpath s ;
drawpoints s;
drawpointlabels s;
resetdrawoptions ;
\stopMPcode
\stoptext

Arrondir les angles : cornered

\starttext
\startMPcode
path p ;
p := ((1,0)--(2,0)--(2,2)--(1,2)--(0,1)--cycle) xysized (4cm,2cm) ;
drawpath p ;
drawpoints p ;
q:= (p shifted (10cm,0)) cornered 1cm ;
p := (p shifted (5cm,0)) cornered .5cm ;
drawpath p ;
drawpoints p ;
drawpath q ;
drawpoints q ;
\stopMPcode
\stoptext

La dimension que tu donnes à cornered dépend de la taille de ton dessin dans notre exemple si tu remplace cornered 1cm par cornered 3cm tu obtiens du grand n'importe quoi.

Simplifier un chemin : simplified

\starttext
\startMPcode
path p ;
p := ((0,0)--(1,0)--(2,0)--(2,1)--(2,2)--(1,2)--(0,2)--(0,1)--cycle) xysized (4cm,2cm) ;
drawpath p ;
drawpoints p ;
p := simplified (p shifted (5cm,0)) ;
drawpath p ;
drawpoints p ;
\stopMPcode
\stoptext

Supprimer des portion de chemin inutile : unspiked

\starttext
\startMPcode
path p ;
p := ((0,0)--(2,0)--(3,1)--(2,0)--(2,2)--(1,2)--(1,3)--(1,2)--(0,1)--cycle) xysized (4cm,2cm) ;
drawpath p ;
drawpoints p ;
p := unspiked (p shifted (5cm,0)) ;
drawpath p ;
drawpoints p ;
\stopMPcode
\stoptext

Rendre un chemin aléatoire : randomized

\starttext
\startMPcode
path p ;
p := fullsquare scaled 2cm ;
drawpath p ;
drawpoints p ;
p := (p shifted (4cm,0)) randomized .5cm ;
drawpath p ;
drawpoints p ;
p := (p shifted (4cm,0)) randomized 1cm ;
drawpath p ;
drawpoints p ;
p := (p shifted (4cm,0)) randomized 2cm ;
drawpath p ;
drawpoints p ;
\stopMPcode
\stoptext

Comprimer un chemin : squeezed

\starttext
\startMPcode
path p ;
p := fullsquare scaled 2cm ;
drawpath p ;
drawpoints p ;
p := (p shifted (4cm,0)) squeezed .5cm ;
drawpath p ;
drawpoints p ;
p := (p shifted (4cm,0)) squeezed 1cm ;
drawpath p ;
drawpoints p ;
p := (p shifted (4cm,0)) squeezed 1.5cm ;
drawpath p ;
drawpoints p ;
\stopMPcode
\stoptext

transformer les courbes en droites : punked

\starttext
\startMPcode
path p ;
p := fullsquare scaled 2cm randomized .5cm ;
drawpath p ;
drawpoints p ;
p := punked (p shifted (4cm,0)) ;
drawpath p ;
drawpoints p ;
path q ;
q := fullcircle scaled 2cm ;
q :=q shifted (8cm,0);
drawpath q ;
drawpoints q ;
q := punked (q shifted (4cm,0)) ;
drawpath q ;
drawpoints q ;
\stopMPcode
\stoptext

transformer les droites en courbes : curved

\starttext
\startMPcode
path p ;
p := fullsquare scaled 2cm;
drawpath p ;
drawpoints p ;
p := curved (p shifted (4cm,0)) ;
drawpath p ;
drawpoints p ;
path q ;
q := fullsquare scaled 2cm randomized .5cm ;
q :=q shifted (8cm,0);
drawpath q ; drawpoints q ;
q := curved (q shifted (4cm,0)) ;
drawpath q ;
drawpoints q ;
\stopMPcode
\stoptext

transformer les courbes en échelons : laddered

\starttext
\startMPcode
path p ;
p := fullcircle scaled 3cm;
drawpath p ;
drawpoints p ;
p := laddered (p shifted (4cm,0)) ;
drawpath p ;
drawpoints p ;
\stopMPcode
\stoptext

\starttext
\startMPcode
path p;
p := (0,0)--(1.4cm,.8cm)--(2.8cm,1.2cm)--(6.2cm,1.6cm) ;
drawpath p
; drawpoints p ;
p:=laddered p;
drawpath p withcolor red;
drawpoints p ;
\stopMPcode
\stoptext

Chemins parallèles : paralleled et blownup

paralleled permet de créer des figures parallèles et concentriques irrégulières ( randomized), dans la figure ci-dessous le chemin originel est en rouge

\starttext
\startMPcode
path p ;
p:=(0,0) .. (3cm,2cm) -- (3cm,4cm) -- (0,4cm)..cycle;
drawpath p withcolor red;
drawpoints p ;
p := p paralleled .5cm ;
drawpath p ;
drawpoints p ;
\stopMPcode
\stoptext

Si l'on utilise un chiffre négatif le nouveau chemin concentrique se fait vers l'extérieur :

\starttext
\startMPcode
path p ;
p:=(0,0) .. (3cm,2cm) -- (3cm,4cm) -- (0,4cm)..cycle;
drawpath p withcolor red;
drawpoints p ;
p := p paralleled -.5cm ;
drawpath p ;
drawpoints p ;
\stopMPcode
\stoptext

Voici un autre exemple :

\starttext
\startMPcode
path p ;
p := fullcircle scaled 3cm ;
drawpath p withcolor red ;
drawpoints p ;
p := p paralleled -1cm ;
drawpath p ;
drawpoints p ;
p := p paralleled -1cm ;
drawpath p ;
drawpoints p ; p := p paralleled -1cm ;
drawpath p ;
drawpoints p ;
\stopMPcode
\stoptext

blownup fait la même chose que paralleled , mais la figure reste identique. L'autre différence est que des chiffres positifs crée une image concentrique vers l'extérieur et des chiffres négatifs vers l'intérieur.

\starttext
\startMPcode
path p ;
p:=(0,0) .. (3cm,2cm) -- (3cm,4cm) -- (0,4cm)..cycle;
drawpath p withcolor red;
drawpoints p ;
p := p blownup .5cm ;
drawpath p ;
drawpoints p ;
p := p blownup .5cm ;
drawpath p ;
drawpoints p ;
p := p blownup .5cm ;
drawpath p ;
drawpoints p ;
\stopMPcode
\stoptext

Des rectangles à coins ronds roundedsquare

\starttext
\startMPcode
path p ;
p := roundedsquare(2cm,4cm,.25cm) ;
drawpath p ;
drawpoints p ;
path q ;
q := roundedsquare(2cm,4cm,.5cm) shifted (3cm,0);
drawpath q ;
drawpoints q ;
\stopMPcode
\stoptext

Des rectangles comme des cercles (ou des ovales) ou des cercles comme des rectangles tensecircle

\starttext
\startMPcode
path p ;
p := tensecircle(2cm,4cm,.25cm) ;
drawpath p ;
drawpoints p ;
path q ;
q := tensecircle(2cm,4cm,.5cm) shifted (3cm,0);
drawpath q ;
drawpoints q ;
\stopMPcode
\stoptext

Des croix crossed

\starttext
\startMPcode
path p, q,r;
p := origin crossed 2cm ;
drawpath p ;
drawpoints p ;
q := (2cm,3cm) crossed 1cm ;
drawpath q ;
drawpoints q ;
r:= (4cm,0) crossed 1cm;
drawpath r rotatedaround (center r,45);
drawpoints r rotatedaround (center r,45);
\stopMPcode
\stoptext

origine est une commande MetaPost qui remplace (0,0).

Dessiner des points n'importe où sur un chemin

La commande draw point of est une commande MetaPost qui permet de dessiner un point entre deux points du chemin :

\starttext
\startMPcode
path p;
p:=(0,0) .. (3cm,2cm) -- (3cm,4cm) -- (0,4cm)..cycle;
drawpath p ;
drawpoints p;
drawpointlabels p;
draw point 0.256 of p withpen pencircle scaled 5mm withcolor .625red ;
draw point 2.5 of p withpen pencircle scaled 5mm withcolor .625green ;
\stopMPcode
\stoptext

MetaFun ajoute deux autres opérateur, on et along

Avec on tu places un point à la distance fournie par rapport à l'origine du chemin. Avec along tu places le point à la fraction de la longueur du chemin. Dans l'exemple ci-dessous je vais placer un point rouge à 3 cm du début du dessin et un point vert à la moitié (1/2=0.5).

\starttext
\startMPcode
path p;
p:=(0,0) .. (3cm,2cm) -- (3cm,4cm) -- (0,4cm)..cycle;
drawpath p ;
drawpoints p;
drawpointlabels p;
draw point 3cm on p withpen pencircle scaled 5mm withcolor .625red ;
draw point 0.5 along p withpen pencircle scaled 5mm withcolor .625green ;
\stopMPcode
\stoptext

Un autre exemple avec une droite et des points tous les centimètres :

\starttext
\startMPcode
path p;
p:=(0,0) -- (1cm,0cm) -- (2cm,0) -- (3cm,0)-- (4cm,0);
drawpath p ;
drawpoints p;
drawpointlabels p;
draw point 3cm on p withpen pencircle scaled 5mm withcolor .625red ;
draw point 0.5 along p withpen pencircle scaled 5mm withcolor .625green ;
\stopMPcode
\stoptext