wood pattern with metapost

I just learned about the fantastic MetaPost package fiziko created by @sergey-slyusarev.

The code (I use ConTeXt to generate this minimal example, it will be similar using LaTeX)

\startMPpage[offset=2bp]
input fiziko.mp
draw woodBlock(10cm,1cm);
\stopMPpage

generates the output

pic with wood pattern

I have not looked into the code to see how easily one could change the form from a rectangle to an arbitrary closed path.


In addition to @mickep 's answer: Sadly, there was no macro to automatically fit a woodBlock into an arbitrary path, now i've added one ( https://github.com/jemmybutton/fiziko/commit/bd24d7fa1144c722ede76e1b75378dc4ea0c1c50 ) You give it a closed path and an angle as arguments and it returns a wood texture picture (without the outline; upd: actually, no, let the outline be). For example, the following code produces something similar to the picture in the question:

input fiziko.mp; 
pair A, B, C, D, E, F, C', E', F';
numeric totalWidth, width, height, breadth, a[];
path p[];
totalWidth := 5cm;
width := 2cm;
height := 3cm;
breadth := 1/3cm;
A := (0, 0);
B := (totalWidth, 0);
C := (1/2totalWidth, 0);
E := (xpart(C), height);
D := 3/4[C, E];
F := (xpart(E) + width, 0);
C' = whatever[C shifted (0, breadth), F shifted (0, breadth)] 
   = whatever[C shifted (breadth, 0), E shifted (breadth, 0)];
E' = whatever[E shifted (breadth, 0), C shifted (breadth, 0)] 
   = whatever[E shifted ((unitvector(E-F) scaled breadth) rotated 90), F shifted ((unitvector(E-F) scaled breadth) rotated 90)];
F' = whatever[C shifted (0, breadth), F shifted (0, breadth)] 
   = whatever[E shifted ((unitvector(E-F) scaled breadth) rotated 90), F shifted ((unitvector(E-F) scaled breadth) rotated 90)];
p1 := A -- B -- B shifted (0, -breadth) -- A shifted (0, -breadth) -- cycle;
a1 := 0;
p2 := C -- E -- E' -- C' -- cycle;
a2 := 90;
p3 := E -- F -- F' -- E' -- cycle;
a3 := angle (E-F);
p4 := C -- F -- F' -- C' -- cycle;
a4 := 0;
for i := 1 step 1 until 4:
    draw woodenThing(p[i], a[i]);
    % draw p[i];
endfor;
dotlabel.top("A", A);
dotlabel.urt("B", B);
dotlabel.ulft("C", C);
dotlabel.lft("D", D);
dotlabel.ulft("E", E);

enter image description here

There are also some global variables which affect the texture to play with: https://github.com/jemmybutton/fiziko/blob/master/fiziko.mp#L1660


Here's the beginning of an answer. More work needed to make it robust and easily re-usable....

A Metapost plank

prologues := 3;
outputtemplate := "%j%c.eps";

vardef wavy expr pa = 
    point 0 of pa
    for a = s step s until arclength(pa):
        .. point arctime(a) of pa of pa 
           shifted (unitvector(direction arctime(a) of pa of pa) 
                   rotated 90 scaled 1/8 normaldeviate)
    endfor
enddef;

beginfig(1);
    numeric s; s = 10;
    path p, q; 
    p = (left--right) scaled 200;
    for i=-10 upto 10:
        draw wavy p shifted (2i*up) rotated -5 
            withpen pencircle xscaled 1/2 yscaled 1/8 rotated 15
            withcolor 1/256(79,36,19);
    endfor

    q = unitsquare shifted -(1/2,1/2) xscaled 144 yscaled 21; 
    clip currentpicture to q; undraw q withpen pencircle scaled 1; draw q;

endfig;
end.