<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html>  <head>   <title>tortue.mp</title>   <link rel="stylesheet" type="text/css" href="/syracuse/fabrique/styles/fabmetapost.css">  </head>  <body>   <a name="haut">   <h1 class="std">tortue.mp</h1>   <hr>    <table width="100%"><tr><td> <pre class="codemp"> <span style="text-decoration:underline;text-weight:bold;color:black;">Fichier : tortue.mp</span>  <SPAN STYLE="font-family:courier,monospace"> <FONT color="gray">% tortue.mp </FONT><FONT color="gray">% La tortue de 11 f&eacute;vrier 2003 </FONT><FONT color="gray">% -------------------------------------------------------------------- </FONT><FONT color="gray">% Jean-Michel Sarlat (http://melusine.eu.org/syracuse/metapost/) </FONT><FONT color="gray">% -------------------------------------------------------------------- </FONT> <FONT color="gray">%% Les tables (chemin,point actuel,orientation,direction) </FONT><FONT COLOR=green>path</FONT> __p[]; <FONT COLOR=green>pair</FONT> __xy[] ; <FONT COLOR=green>numeric</FONT> __m[], __a[];  <FONT color="gray">%% L'enregistrement d'une tortue (initialisation) </FONT><FONT COLOR=blue><B>def</B></FONT> tortue(<FONT COLOR=navy>expr</FONT> n, x , y , a , m ) =     __xy[n] := (x,y) ; __a[n] := a ; __m[n] := m ; __p[n] := (x,y); <FONT COLOR=blue><B>enddef</B></FONT>;  <FONT color="gray">%% La tortue &lt;n&gt; se d&eacute;double, &lt;m&gt; &agrave; cet instant se superpose &agrave; &lt;n&gt; </FONT><FONT COLOR=blue><B>def</B></FONT> blop(<FONT COLOR=navy>expr</FONT> n,m) =     tortue(m,xpart __xy[n],ypart __xy[n],__a[n],__m[n]); <FONT COLOR=blue><B>enddef</B></FONT>;   <FONT color="gray">%% La tortue tourne (sur place) </FONT><FONT COLOR=blue><B>def</B></FONT> tourne(<FONT COLOR=navy>expr</FONT> n, a ) =     __a[n] := ( __a[n] + a * __m[n] ) <FONT COLOR=maroon>mod</FONT> 360; <FONT COLOR=blue><B>enddef</B></FONT>;   <FONT color="gray">%% La tortue se retourne, sa gauche est notre droite ! </FONT><FONT COLOR=blue><B>def</B></FONT> retourne(<FONT COLOR=navy>expr</FONT> n ) =     __m[n] := - __m[n]; <FONT COLOR=blue><B>enddef</B></FONT>;  <FONT color="gray">%% La tortue avance d'une certaine distance et fait ce qu'on lui demande </FONT><FONT COLOR=blue><B>vardef</B></FONT> avance(<FONT COLOR=navy>expr</FONT> n,l)(<FONT COLOR=navy>suffix</FONT> faire) =     <FONT COLOR=blue><B>save</B></FONT> position;     <FONT COLOR=green>pair</FONT> position;     position =  __xy[n] + ((l,0) <FONT COLOR=red>rotated</FONT> __a[n]);     faire(__xy[n],position);     __xy[n] := position;     __p[n] := __p[n]--position; <FONT COLOR=blue><B>enddef</B></FONT>;  <FONT color="gray">%% Remplissage du polygone circonscrit par le chemin de la tortue </FONT><FONT COLOR=blue><B>vardef</B></FONT> remplis(<FONT COLOR=navy>expr</FONT> n,c) =     <FONT COLOR=blue>fill</FONT> __p[n]--cycle <FONT COLOR=olive>withcolor</FONT> c; <FONT COLOR=blue><B>enddef</B></FONT>;  <FONT color="gray">%% Une chose que la tortue peut faire en se d&eacute;pla&ccedil;ant : laisser une trace ! </FONT><FONT COLOR=blue><B>vardef</B></FONT> trace(<FONT COLOR=navy>expr</FONT> a,b) =     <FONT COLOR=blue>draw</FONT> a--b; <FONT COLOR=blue><B>enddef</B></FONT>;   <FONT color="gray">%% Une autre chose : suivre un chemin floconneux (tortue ivre mais s&ucirc;re ...) </FONT><FONT COLOR=green>numeric</FONT> indice_koch,iter_koch; indice_koch := 0;  <FONT color="gray">% valeurs par d&eacute;faut </FONT>iter_koch   := 2;  <FONT COLOR=blue><B>vardef</B></FONT> m_koch(<FONT COLOR=navy>expr</FONT> m,d) =  <FONT COLOR=purple>if</FONT> m=0:   avance(indice_koch,d,trace);  <FONT COLOR=purple>else</FONT>:   m_koch(m-1,d);   tourne(indice_koch,60);   m_koch(m-1,d);   tourne(indice_koch,-120);   m_koch(m-1,d);   tourne(indice_koch,60);   m_koch(m-1,d);  <FONT COLOR=purple>fi</FONT> <FONT COLOR=blue><B>enddef</B></FONT>;  <FONT COLOR=blue><B>vardef</B></FONT> koch(<FONT COLOR=navy>expr</FONT> a,b) =   <FONT COLOR=green>numeric</FONT> d_koch;  d_koch = <FONT COLOR=maroon>sqrt</FONT>((xpart (b-a))**2+(ypart (b-a))**2)/(3**iter_koch);  tortue(indice_koch,xpart a,ypart  a,angle(b-a),1);  m_koch(iter_koch,d_koch); <FONT COLOR=blue><B>enddef</B></FONT>;   <B>endinput</B>        </SPAN></pre></td></tr></table>      <a href="javascript:history.back()">          <img src="/syracuse/fabrique/images/retour.png" border="0" alt="retour" /></a>     <hr>   <i>Source      <a href="tortue.txt">tortue.txt</a>      moulin par <b>petitParseur(Z+B)</b>      le jeudi 13 fvrier 2003.</i>  </body> </html> 
