
\startMPinclusions

color pink	;
pink:= (255/255,105/255,180/255) ;


patternsize:= 8mm;
poffset := 0.5*patternsize;

def S = 45 enddef; % TABLET angle when threaded
def Z = 135 enddef; % TABLET angle when threaded

%each thread
def tt(expr rot,ca,sx,sy) =
	picture stitch;
		stitch := image (
			path q;
			q:= fullcircle scaled 10mm xscaled .45;
			draw q withpen pencircle scaled .2mm withcolor .2white;
			fill q withcolor ca;
			);
			draw stitch rotated rot shifted (sx*patternsize,sy*patternsize)
enddef;


% gridlines, nr of horizontal * nr of vertical
def twgrid(expr sgw,sgh) = 
	picture mygrid;
		mygrid := image (
			draw hlingrid (0,sgh*patternsize,patternsize,sgh*patternsize,sgw*patternsize) ;
			draw vlingrid (0,sgw*patternsize,patternsize,sgw*patternsize,sgh*patternsize) ;
			);
	draw mygrid shifted (poffset,-poffset) withcolor .8white ;
enddef;


% background highlight for direction switch/flip
def bgsq(expr sx, sy) =
	picture bgshade;
		bgshade:= image (
		fill unitsquare scaled patternsize withcolor .5white ;		
		);
	draw bgshade shifted ((sx-1/2)*patternsize,(sy-1/2)*patternsize) ;
enddef;


\stopMPinclusions


\startMPpage

nt := 10; % nr of tablets
nh := 4 ; % nr of holes per tablet
nr := 5; % nr of *pattern* repeats

% draw squares on the background (horizontal, vertical)
twgrid (nt,nr*nh) ;

% Labels for holes
label ("A", (0,0)) ;
label ("B", (0,1*patternsize)) ;
label ("C", (0,2*patternsize)) ;
label ("D", (0,3*patternsize)) ;

% Numbering for tablets
label ("1",(1*patternsize,-(1*patternsize))) ;
label ("2",(2*patternsize,-(1*patternsize))) ;
label ("3",(3*patternsize,-(1*patternsize))) ;
label ("4",(4*patternsize,-(1*patternsize))) ;
label ("5",(5*patternsize,-(1*patternsize))) ;
label ("6",(6*patternsize,-(1*patternsize))) ;
label ("7",(7*patternsize,-(1*patternsize))) ;
label ("8",(8*patternsize,-(1*patternsize))) ;
label ("9",(9*patternsize,-(1*patternsize))) ;
label ("10",(10*patternsize,-(1*patternsize))) ;


picture twpattern ;

twpattern :=
	image (
	% tablet 1
	tt (Z,pink,1,0) ;
	tt (Z,pink,1,1) ;
	tt (Z,pink,1,2) ;
	tt (Z,pink,1,3) ;

	% tablet 2
	tt (S,white,2,0);
	tt (S,white,2,1);
	tt (S,white,2,2);
	tt (S,white,2,3);

	% tablet 3
	tt (Z,white,3,0);

	bgsq (3,1); % shading to indicate turning direction switch
	tt (S,white,3,1);
	bgsq (3,2); % shading to indicate turning direction switch
	tt (S,pink,3,2);

	tt (Z,pink,3,3);

	% tablet 4
	tt (S,white,4,0);
	tt (S,pink,4,1);
	tt (S,pink,4,2);
	tt (S,pink,4,3);

	% tablet 5
	tt (S,pink,5,0);
	tt (S,pink,5,1);
	tt (S,pink,5,2);
	tt (S,white,5,3);

	% ====== midpoint =========


	% tablet 6
	tt (Z,pink,6,0);
	tt (Z,pink,6,1);
	tt (Z,pink,6,2);
	tt (Z,white,6,3);


	% tablet 7
	tt (Z,white,7,0);
	tt (Z,pink,7,1);
	tt (Z,pink,7,2);
	tt (Z,pink,7,3);

	% tablet 8
	tt (S,white,8,0);

	bgsq (8,1); % shading to indicate turning direction switch
	tt (Z,white,8,1);
	bgsq (8,2); % shading to indicate turning direction switch
	tt (Z,pink,8,2);

	tt (S,pink,8,3);


	% tablet 9
	tt (Z,white,9,0);
	tt (Z,white,9,1);
	tt (Z,white,9,2);
	tt (Z,white,9,3);

	% tablet 10
	tt (S,pink,10,0);
	tt (S,pink,10,1);
	tt (S,pink,10,2);
	tt (S,pink,10,3);

	); 


%draw twpattern ;


picture prepeat;

	prepeat :=
		image (
		for i=0 step nh*patternsize until ((nr-1)*nh*patternsize) :
			draw twpattern shifted (0,i) ;
		endfor ;
		) ;

draw prepeat ;

rx := (nt+1)*patternsize ;
ry := nr*nh*patternsize - poffset ;	





\stopMPpage

