\startMPpage

drawoptions
	(
		withpen pencircle scaled 9/64 % the width of the braided "ribbon", same for all lines- do not touch!
		withcolor white % the colour of the knotwork, change as you please
	) ; 
linecap := squared ; % to make the line ends straight at the 'cutting' points 


% everything happens on a 4/16/64 step grid


% The corner component
path cornercomponent ;
cornercomponent = (
       (9/16,2+7/16) --
       (1/2,5/2) --
       (1/2,7/2) --
       (3/2,7/2) --
       (2+9/16,2+7/16)
   )
   scaled (1/4) ;

% The edge component
path edgecomponent ;
edgecomponent = (
		(7/16,	3+7/16) --
       (1/2,7/2) --
       (3/2,7/2) --
       (2+9/16,2+7/16)
   )
   scaled (1/4) ;

% The diagonal line in the middle of the braid
path diagcomponent ;
diagcomponent = ( 
		(1+7/16,	1+7/16) --
       (2+9/16,2+9/16)
   )
   scaled (1/4) ;


% the two-loop combo at the end of the figure
vardef endcomp (expr cornercomponent) =
   image (
       draw cornercomponent ;
       draw cornercomponent rotatedaround  ((1,1)), 90) ;
   )
enddef ;


% the middle of long ribbon, a'block' with turning points at edges and straight line inbetween
vardef plait (expr edgecomponent) =
   image (
      draw edgecomponent shifted (1/2	,0);
       draw edgecomponent rotated 180 shifted (1,1) ;
      draw diagcomponent shifted (1/4,0);
   )
enddef ;




% defining the corner block for a frame
def fcorner =
 	image (
		draw cornercomponent; 
       	draw edgecomponent rotated 90 shifted (1, 0) ;
		draw diagcomponent rotated 90 shifted (1, (-1/4)) ;
      	draw edgecomponent shifted (1/2	,0) ;
		draw diagcomponent shifted (1/4, 0) ;
		draw diagcomponent shifted (1/4, -1/2) ;
		)
enddef ;



%%% drawing the frame

ph := 12 ; % nr of plait blocks horizontally
pv := 20 ; % nr of plait blocks vertically


%%% NOTE: my building blocks start from left top, which might have been sligtly stupid as MetaPost thinks from left bottom up. This is why I had to resort to using negative coordinates to get the frame done.


% drawing the corner blocks
draw fcorner  ; % lefttop
draw fcorner rotated 90 shifted (1,-((pv+1)/2)); % leftbottom
draw fcorner rotated 180 shifted (ph/2+2,-(pv-1)/2) ; % rightbottom
draw fcorner rotated 270 shifted (ph/2+1,1) ; % righttop

%the bar on top of page/frame
for i=1/2 step 1/2 until (ph/2) :
	        draw plait (edgecomponent) shifted (i,0) ;
endfor ;


%the bar on bottom of page/frame
for i=1/2 step 1/2 until (ph/2) :
	        draw plait (edgecomponent) shifted (i,-((pv+1)/2)) ;
endfor ;



%the bar on the right on the page
for j=1 step 1/2 until (pv/2) :
	        draw plait (edgecomponent) rotated 90 shifted (1,-j) ;
endfor ;

%the bar on the right on the page
for j=1 step 1/2 until (pv/2) :
	        draw plait (edgecomponent) rotated 90 shifted ((ph/2)+2,-j) ;
endfor ;



% adding space around the knot-and-plait picture
setbounds currentpicture to boundingbox currentpicture enlarged 1/16 ;

% colouring the background black
addbackground withcolor black ;


% this clears the middle; instead of doing a white fill, I remove the black, so the frame really is just a frame.
unfill (17/16,-1/16) -- ((ph/2+15/16),-1/16) -- ((ph/2+15/16),-(pv-1)/2+1/16) -- (17/16,-(pv-1)/2+1/16) -- cycle;


% resizing the whole thing to be 5 cm wide (ratio is kept, height is whatever it becomes in scali)
currentpicture := currentpicture xsized 20cm ;

\stopMPpage
