numeric wd; wd=210mm; % Paper width. numeric ht; ht=297mm; % Paper height. numeric marg; marg=2cm; % Margin left at each end of paper. def recurse_koch(expr pstart, pend, depth) = if depth=0: p := p--pend; else: recurse_koch(pstart, 1/3[pstart,pend], depth-1); recurse_koch(1/3[pstart,pend], 1/3[pstart,pend] + ((1/3[pstart,pend]-pstart) rotated 60), depth-1); recurse_koch(1/3[pstart,pend] + ((1/3[pstart,pend]-pstart) rotated 60), 2/3[pstart,pend], depth-1); recurse_koch(2/3[pstart,pend], pend, depth-1); fi enddef; % 2 Koch Curves. % Assuming a 0 level one is a single line, these two are 3 and 6 levels deep. beginfig(1); numeric triht; triht=xpart (((ht-2marg)/3)*dir (90-60)); numeric gap; gap=(wd-2*triht)/3; numeric base; base=gap+triht; pickup pencircle scaled 2.3pt; path p; p=(base, marg); recurse_koch((base, marg), (base, ht-2marg), 3); draw p; numeric base; base=wd-gap; pickup pencircle scaled 0.23pt; path p; p=(base, marg); recurse_koch((base, marg), (base, ht-2marg), 6); draw p; endfig; % A Koch Snowflake, made of three Koch Curves arranged in a triangle. beginfig(2); z0=(wd/2,ht/2); numeric r; z1=z0+(r*dir (90)); x2=marg; z2=z0+(r*dir (90+120)); z3=z0+(r*dir (90+240)); pickup pencircle scaled 1.3pt; path p; p=z2; % Left side. recurse_koch(z2, z1, 5); path pl; pl=p; path p; p=z1; % Right side. recurse_koch(z1, z3, 5); path pr; pr=p; path p; p=z3; % Bottom. recurse_koch(z3, z2, 5); path pb; pb=p; % Join the 3 sides to get a single closed path. path pw; pw=pl--pr--pb--cycle; fill pw withcolor (144/255,238/255,144/255); draw pw; endfig; end