#!/usr/local/bin/perl -w
# tangles.p (formerly known as tangle.p)
# is a perl program that writes metapost files for tangles
# as numbered by John Conway
#
# tangles.p (c) David MacKay  Tue 11/3/03  is provided as free software
# under the GNU General Public License
# http://www.gnu.org/copyleft/gpl.html

# If invoked with no command line argument then a sequence of tangles is
# created. Use the command-line argument
# tangles.p k=uuuururruruuruurr
# or
# tangles.p k=ruu texout=infinity mpout=infinity
# tangles.p k=uur texout=uur mpout=uur
# tangles.p k=uuu texout=uuu mpout=uuu
# (edit figs 6 and 7 of these in order to make the generic pictures)
#
# to create the tangle sequence corresponding to that sequence of "up"s and "round"s.
#
# If you use the default settings then the finished document can be created using: 
#  tangles.p ; mpost mpfile ; latex texfile ; dvips texfile ; gv texfile.ps
#
# While purists such as John Conway make all their tangles using only
# "u" and "r", I have added a "d" option Wed 9/6/04 for people
# who want to be able to do "twist em down".

$k="uuuururruruuruuru" ; # specifies the knot (tangle) to make, by a sequence of "u" (twist'em up) and "r" (turn 'em 'roun')
    
#
# Define a load of modifiable variables
#
$fignum=1; # number of the first figure
$usewhite = 1 ; # whether to create white space around each rope to make crossings look nice
@usewhitecentre=(1,0);

@color=(0,1);
@colorname=("red","blue") ;
@colorname=("0.5 white","blue") ;
@colorname=("black","blue") ;

$showdancers = 1 ; # whether to show the four dancers
$dancerstext = ' % define dancer symbols
       u:=2.5mm ; path d[] ;
       d4 := unitsquare shifted (-0.5,-0.5) scaled u; path unittri ;
       path cunittri ; unittri := (0,0)--(1,0)--(0.5,sqrt(3)/2.0)--(0,0)--cycle ;
       cunittri := unittri shifted  (- ( (0,0)+(1,0)+(0.5,sqrt(3)/2.0 ) ) /3.0 );
       d1 := cunittri scaled 1.5u; d2 := d4 rotated 45 ; d3 := fullcircle  scaled 1.2u ;
' ;


$wraptex = 1 ; # whether to include head and tail in the tex file.
$clearpages = 0 ; # whether to emit occasional clear page commands in tex file
$u="3.4mm"; # fundamental unit.
$uname="u"; # name of fundamental unit in metapost
$state_the_pens = "% pens\n centrepen:=1.5pt; ropepen:=2.5pt; gappen := 4pt ; arrowpen := 1pt; \n" ; 
$linepen = "pickup defaultpen; " ;
$centrepen = "pickup pencircle scaled centrepen;" ;
$ropepen = "pickup pencircle scaled ropepen;" ;
$gappen = "pickup pencircle scaled gappen;" ; # pen to create white around rope
$arrowpen = " pickup pencircle scaled arrowpen;" ; 
$lambda = 0.5 ; # param between 0 and 1 that determines placement of new y coordinates when we "twist em up"

$xgap = 1.5 ; $ygap = 1 ; # how far away from the tangle the dancers always stand
$minxseparation=6.9; # how far from each other the dancers always stand
$minyseparation=4.9;


# startingconfig
$r=0 ; $b=1 ;
# starting state: the entire tangle is just two dots : with no length 
$x{$r,0}=0;$y{$r,0}=1; 
$x{$b,0}=0;$y{$b,0}=0; 
# Here, use  0 and 1 to denote the two ends of the whole path

$mpout="mpfile" ; # ".mp" suffix will be added
$texout="texfile" ; # ".tex" will be added
$datout="dat" ; 
$section="Tangles by tangles.p" ;

$verbose = 0 ; # for debugging 

# optional overwriting of "(t)" on top of the central tangle.
# done by hand
# $showtblob = 0 ; 

# optional additional arrows showing motion of the troup.
$showarrows = 2 ; # if 1, shows arrows only on the first diagram
$arrowcolor = '0.50green' ;
sub uparrows () {
    print MP " path uparrow[],qup[] ;\n $arrowpen \n" ;
#    print MP "   pair midpointl, midpointr, toppoint; \n" ;
    if (0) { # original style
	$toppointy = (1.12 * $tside -0.12* $bside) ;
	$toppointx = $rside - 0.105 * ($tside-$bside) ;  
	$midpointl = $rside - 0.2 * ($tside-$bside) ; 
	$midpointr = $rside + 0.3 * ($tside-$bside) ;
    } else {	# new style, to look better with big pictures 
	$toppointy = (1.12 * $tside -0.12* $bside) ;
	$toppointx = $rside - 0.25 * ($minyseparation);
	$midpointl = $rside - 0.1 * ($minyseparation) ;
	$midpointr = $rside + 0.3 * ($minyseparation) ;
    }
    $midpointy = 0.5* ($tside + $bside) ;
    $midpointyl =  (0.4*$tside + 0.6*$bside) ;
    if(0){ # old style 
	print MP "    uparrow2 := $dancer{3}..($midpointl,$midpointyl)..($toppointx,$toppointy)..$dancer{2}..($midpointr,$midpointy)..cycle ;\n" ; 
	print MP " qup0 = uparrow2 scaled $uname ;\n" ;
	print MP " qup1 = subpath (3,5) of uparrow2 scaled $uname ;\n" ;
	print MP " drawarrow subpath (0.28,2.2) of qup0 withcolor $arrowcolor ;\n" ;
	print MP " drawarrow subpath (0.22,1.8) of qup1 withcolor $arrowcolor ;\n" ;
    } else {
	print MP "    uparrow2 := $dancer{3}"."{-0.2,1}..($toppointx,$toppointy){0,1}..$dancer{2}..($midpointr,$midpointy)..$dancer{3} ;\n" ; 
	print MP " qup0 = uparrow2 scaled $uname ;\n" ;
	print MP " qup1 = subpath (2,4) of uparrow2 scaled $uname ;\n" ;
	print MP " drawarrow subpath (0.08,1.75) of qup0 withcolor $arrowcolor ;\n" ;
	print MP " drawarrow subpath (0.22,1.8) of qup1 withcolor $arrowcolor ;\n" ;
    }	
}
sub downarrows () {
    print MP " path uparrow[],qup[] ;\n $arrowpen \n" ;
	$toppointy = (1.12 * $bside -0.12* $tside) ;
	$toppointx = $rside - 0.25 * ($minyseparation);
	$midpointl = $rside - 0.1 * ($minyseparation) ;
	$midpointr = $rside + 0.3 * ($minyseparation) ;
    $midpointy = 0.5* ($tside + $bside) ;
    $midpointyl =  (0.4*$tside + 0.6*$bside) ;
	print MP "    uparrow2 := $dancer{2}"."{-0.2,-1}..($toppointx,$toppointy){0,-1}..$dancer{3}..($midpointr,$midpointy)..$dancer{2} ;\n" ; 
	print MP " qup0 = uparrow2 scaled $uname ;\n" ;
	print MP " qup1 = subpath (2,4) of uparrow2 scaled $uname ;\n" ;
	print MP " drawarrow subpath (0.08,1.75) of qup0 withcolor $arrowcolor ;\n" ;
	print MP " drawarrow subpath (0.22,1.8) of qup1 withcolor $arrowcolor ;\n" ;
}
sub roundarrows () {
    print MP " path roundarrow , qr ;\n $arrowpen\n" ;
    print MP " roundarrow = $dancer{1}..$dancer{2}..$dancer{3}..$dancer{4}..$dancer{1}..cycle ; \n" ;
    print MP " qr = roundarrow scaled $uname ;\n" ;
    print MP " drawarrow subpath (0.1,0.9) of qr withcolor $arrowcolor; \n" ; 
    print MP " drawarrow subpath (1.2,1.8) of qr withcolor $arrowcolor; \n" ; 
    print MP " drawarrow subpath (2.1,2.9) of qr withcolor $arrowcolor; \n" ; 
    print MP " drawarrow subpath (3.2,3.8) of qr withcolor $arrowcolor; \n" ; 
}

# initial configuration ... not to be changed
# where the people are.
#  1-2 Red
#  4-3 Blue
$whois[1] = 1 ; 
$whois[2] = 2 ; 
$whois[3] = 3 ; 
$whois[4] = 4 ; 
# first argument is the end we are talking about
# second argument is the rope color
$whereis{0,0} = 1 ; 
$whereis{1,0} = 2 ; 
$whereis{0,1} = 4 ; 
$whereis{1,1} = 3 ; 
# argument of this is a person 1,2,3,4
$colorof[1] = 0 ; 
$colorof[2] = 0 ; 
$colorof[3] = 1 ; 
$colorof[4] = 1 ;
# argument of this is a person 1,2,3,4
$isatend[1] = 0 ; 
$isatend[2] = 1 ; 
$isatend[3] = 1 ; 
$isatend[4] = 0 ;
# first argument is the end we are talking about
# second argument is the rope color
$length{0,0} = 0; # the number of edges between the starting : and ending : of each rope
$length{0,1} = 0; # the number of edges between the starting : and ending :
$length{1,0} = 0; # the number of edges between the starting : and ending : of each rope
$length{1,1} = 0; # the number of edges between the starting : and ending :

# You might want to change these if you want the tangle to look more square symmetric
# rather than slightly rectangular :=:
#
# these tangents belong to places 1 2 3 4, with the second argument
# being which end of a path we are at.
$tangent{1,0}="{(2,-1)}" ;
$tangent{2,1}="{(2,1)}" ;
$tangent{3,1}="{(2,-1)}" ;
$tangent{4,0}="{(2,1)}" ;
$tangent{1,1}="{(-2,1)}" ;
$tangent{2,0}="{(-2,-1)}" ;
$tangent{3,0}="{(-2,1)}" ;
$tangent{4,1}="{(-2,-1)}" ;

eval "\$$1=\$2" while @ARGV && $ARGV[0]=~ /^(\w+)=(.*)/ && shift;

@K = split ( '' , $k ) ;
print STDERR "there are $#K instructions in $k: $K[0] $K[1] $K[2] $K[3] $K[4]...\n"  if ($verbose)  ;

$edge = 0 ; 

$t = 0 ; # initial value of the variable "t".
$tnum = 0 ; $tdenom = 1 ;
cleanupt();
# ensure tdenom always positive.
sub cleanupt {
    if ($tdenom <0) { $tdenom = -$tdenom ; $tnum = -$tnum ; }
    if ($tdenom ==1 ) { $tstring = "$tnum" ; } else {    $tstring = "$tnum/$tdenom" ; }
    print STDERR "t = $tstring\n" if ($verbose) ; 
}
sub addone { # t -> t+1
    $tnum += $tdenom ;
    cleanupt() ;
    $t ++ ; # floating point number not used.
}
sub subtractone { # t -> t-1
    $tnum -= $tdenom ;
    cleanupt() ;
    $t -- ; # floating point number not used.
}
sub invertminus { # t -> -1/t
    $newtnum = - $tdenom ; 
    $tdenom = $tnum ;
    $tnum = $newtnum ;
    cleanupt() ;
    if ( $t != 0.0 ) {
	$t = -1.0/$t ;
    }
}

# initialize variables
$index = -1 ; # runs over curves in the gnuplot file
$fignum -- ;

# open all files
# optional output file with raw coordinates in it.
open ( DAT , ">$datout" ) ; $gnustring = "set nokey\n" ;

open ( MP , ">$mpout.mp" ) ;
print MP '% metapost document created by tangles.p - Free Software (c) by David MacKay 2003'."\n" ;
# make some general definitions
if ($showdancers) { print MP $dancerstext ; }
print MP "% the fundamental unit for tangle diagrams\n $uname:=$u; \n" ; 
print MP $state_the_pens ;

open ( TEX , ">$texout.tex" ) ;
if ($wraptex) {
    print TEX '
\documentclass{article}
\twocolumn
% latex document created by tangles.p - Free Software (c) by David MacKay 2003
\usepackage{epsf}
\oddsidemargin=-0.30in
\textwidth=7.0in
\topmargin=-0.5in
\textheight=9.5in
\begin{document}
%\newcommand{\dofigure}[3]{\begin{figure}[htbp]\[\epsfbox{#2}\]\caption[a]{#3}\label{fig#1}\end{figure}}
\newcommand{\dofigure}[3]{\begin{tabular}{c}
 \mbox{\epsfbox{#2}} \\\\
 {#1: #3}\\\\
%\label{fig#1}
\end{tabular}}
\newcommand{\twistemup}{twist\'em up}
\newcommand{\twistemdown}{twist\'em down}
\newcommand{\turnemround}{turn\'em roun\'}
\section*{'.$section.'}
\begin{center}
';
}

if ($showarrows==0) {
# draw all figures, simply
    drawtangle(0) ; 
    foreach $k (@K) {
	if ( $k eq "u" ) { up(); }
	elsif ( $k eq "d" ) { down(); }
	else { round(); }
	drawtangle(0) ; 
    }
} else {
# draw all figures, finding what the next instruction is first 
    foreach $k (@K) {  # hope $k is a global variable, as it is used in drawtangle
	drawtangle(0) ;                   # show the picture
	drawtangle($k) if ($showarrows) ; # show the picture with arrows
	if ( $k eq "u" ) { up(); }
	elsif ( $k eq "d" ) { down(); }
	else { round(); }
	if ($showarrows==1) { $showarrows=0; # switch off showing of arrows after the first picture
			  }
    }
    drawtangle(0) ; 
}

# closing comments
if ($wraptex) {
    print TEX '\end{center}'."\n" ;
    print TEX '\end{document}'."\n" ;
}
print MP "end\n" ;
# done closing comments
print STDERR "mpost $mpout ; latex $texout ; dvips $texout ; gv $texout.ps\n" ;
print STDERR "gnuplot\n $gnustring" if ($verbose) ; 

# always invoke drawtangle (to find max and min) before this.
sub round { ## turn'em 'round
    $w = $whois[4] ;
    $whois[4] = $whois[3] ; 
    $whois[3] = $whois[2] ; 
    $whois[2] = $whois[1] ; 
    $whois[1] = $w ; 

    foreach $e (0,1) {
	foreach $c (0,1) { # 
	    $whereis{$e,$c}  ++ ;	# 
	    if ( $whereis{$e,$c} > 4 ) { $whereis{$e,$c}  -= 4 ; }
	}
    }

    foreach $c (@color) {
	for ( $l = $length{0,$c} ; $l <= $length{1,$c} ; $l ++ ) {
	    $xx = $x{$c,$l} ;	# 
	    $yy = $y{$c,$l} ;	# 
	    $x{$c,$l} =  $yy ; $y{$c,$l} = (-$xx)  ;
	}
    }
    $w = $ywidth ;    $ywidth = $xwidth ;  $xwidth = $w ;
    # other variables xmax xmin ymax ymin are now incorrect. Need to run drawtangle immediately
    invertminus() ; #    $t = -1.0/$t ;
}
    
# always invoke drawtangle (to find max and min) before this.
sub up { ## twist'em up
    # adds an extra item to the right hand side.

    $whoupstage   = $whois[2] ;
    $whodownstage = $whois[3] ;

    # first, downstage is moved upstage
    $c=    $colorof[$whodownstage] ;
    $end = $isatend[$whodownstage] ;
    if ( $end == 0 ) {  $length{$end,$c} -- ; } else {  $length{$end,$c} ++ ;  }
    $l = $length{$end,$c} ;
    $xstep = ($ywidth)/2.0 ; if($xstep<1.0) { $xstep = 1; }
    $x{$c,$l} = $xmax+$xstep ; $y{$c,$l} = $lambda * $ymax + (1.0-$lambda) * ($ycentre + 0.5*$xstep) ;
    $edge ++ ;
    $orderc[$edge] = $c ;     if ( $end == 0 ) {  $l++ ; }  $orderl[$edge] = $l ; # keep track of the order in which they should be drawn. (the increment by 1 is to ensure that the label associated with each edge is the integer of its down-path end
    # update locations of this person
    $whereis{$end,$c}  = 2    ;

    # second, upstage to downstage, crossing over + above. like this  ,\'
    $c=    $colorof[$whoupstage] ;
    $end = $isatend[$whoupstage] ;
    if ( $end == 0 ) {  $length{$end,$c} -- ; } else {  $length{$end,$c} ++ ;  }
    $l = $length{$end,$c} ;
    $x{$c,$l} = $xmax+$xstep ; $y{$c,$l} = $lambda * $ymin + (1.0-$lambda) * ($ycentre - 0.5*$xstep) ;
    $edge ++ ;
    $orderc[$edge] = $c ;     if ( $end == 0 ) {  $l++ ; } $orderl[$edge] = $l ; # keep track of the order in which they should be drawn.


    # update locations of people
    $whois[2] =    $whodownstage  ;
    $whois[3] =    $whoupstage ;
    # update locations of people
    $whereis{$end,$c}  = 3    ;

    addone(); # $t ++ ; 
}

# always invoke drawtangle (to find max and min) before this.
sub down { ## twist'em down
    print STDERR "warning, 'down' not tested yet'\n";
    # adds an extra item to the right hand side.

    $whoupstage   = $whois[2] ;
    $whodownstage = $whois[3] ;

    $xstep = ($ywidth)/2.0 ; if($xstep<1.0) { $xstep = 1; }

    # first

    $c=    $colorof[$whoupstage] ;
    $end = $isatend[$whoupstage] ;
    if ( $end == 0 ) {  $length{$end,$c} -- ; } else {  $length{$end,$c} ++ ;  }
    $l = $length{$end,$c} ;
    $x{$c,$l} = $xmax+$xstep ; $y{$c,$l} = $lambda * $ymin + (1.0-$lambda) * ($ycentre - 0.5*$xstep) ;
    $edge ++ ;
    $orderc[$edge] = $c ;     if ( $end == 0 ) {  $l++ ; } $orderl[$edge] = $l ; 
    # update locations of people
    $whereis{$end,$c}  = 3    ;

    # second
        $c=    $colorof[$whodownstage] ;
    $end = $isatend[$whodownstage] ;
    if ( $end == 0 ) {  $length{$end,$c} -- ; } else {  $length{$end,$c} ++ ;  }
    $l = $length{$end,$c} ;
    $x{$c,$l} = $xmax+$xstep ; $y{$c,$l} = $lambda * $ymax + (1.0-$lambda) * ($ycentre + 0.5*$xstep) ;
    $edge ++ ;
    $orderc[$edge] = $c ;     if ( $end == 0 ) {  $l++ ; }  $orderl[$edge] = $l ; 
    # update locations of this person
    $whereis{$end,$c}  = 2    ;


    # update locations of people
    $whois[2] =    $whodownstage  ;
    $whois[3] =    $whoupstage ;

    subtractone(); # $t ++ ; 
}

sub drawtangle { # usage ("") or ("u") or ("r") depending whether you want the next move labelled with arrows or not
    local ( $k ) = @_ ; 
    
    # open the figure     ########################
    print DAT "# t=$t \n" ; 
    print TEX "\%    t = $t \n" ;
    print MP  "\%    t = $t \n" ;
    $fignum ++ ;
    print MP  "beginfig($fignum);\n" ;
    if ( $k eq "u" ) {
	$extra =  " -- \\twistemup" ;
    } elsif ( $k eq "r" ) {
	$extra =  " -- \\turnemround" ;
    } elsif ( $k eq "d" ) {
	$extra =  " -- \\twistemdown" ;
    } else { $extra = "" ; }
    
#    $tstring = sprintf "%6f", $t ; 
    print TEX '\dofigure{'.$fignum.'}{'."$mpout.$fignum".'}{$t='."$tstring\$$extra\}\n" ; 
    if ( $clearpages &&  !($fignum%$clearpages) ) { print TEX '\clearpage'."\n" ; }
    # done opening the figure ####################

    # run through the sequence of points and find the extremes.

    $xmax = 0 ; $xmin = 0 ; $ymax = 0 ; $ymin = 0 ;
    foreach $c (@color) {
	for ( $l = $length{0,$c} ; $l <= $length{1,$c} ; $l ++ ) {
	    $xx = $x{$c,$l} ;
	    if ($xx>$xmax) { $xmax = $xx ; }
	    if ($xx<$xmin) { $xmin = $xx ; }
	    $yy = $y{$c,$l} ;
	    if ($yy>$ymax) { $ymax = $yy ; }
	    if ($yy<$ymin) { $ymin = $yy ; }
	}
    }
    $xcentre = 0.5*($xmax+$xmin) ;
    $ycentre = 0.5*($ymax+$ymin) ;
    $xwidth  = ($xmax-$xmin) ;
    $ywidth  = ($ymax-$ymin) ;
    print "Knot centre at $xcentre $ycentre\n" if ($verbose) ;

    # Determine where to put the 4 dancers.
    $rside = $xmax + $xgap ;
    $lside = $xmin - $xgap ;
    $tside = $ymax + $ygap ;
    $bside = $ymin - $ygap ;
    $xsep = - ($rside - $lside) + $minxseparation;
    if ( $xsep > 0.0 ) {
	$rside += $xsep / 2.0 ;
	$lside -= $xsep / 2.0 ;
    }
    $ysep = - ($tside - $bside) + $minyseparation;
    if ( $ysep > 0.0 ) {
	$tside += $ysep / 2.0 ;
	$bside -= $ysep / 2.0 ;
    }
    # 1234 are here the four corners, fixed.
    $dancer{1} = "($lside,$tside)" ;
    $dancer{2} = "($rside,$tside)" ;
    $dancer{4} = "($lside,$bside)" ;
    $dancer{3} = "($rside,$bside)" ;
    for ($d=1;$d<=4;$d++) { # $dance is a coordinate that includes the tangent also. The tangent depends on which end of the rope it is
	$dance{$d} = "$dancer{$d}$tangent{$d,$isatend[$whois[$d]]}" ;
    }

    if ($showarrows) { # add up or round arrows \n" ;
	if ( $k eq "u" ) {
	    uparrows();
	} elsif ( $k eq "d" ) {
	    downarrows();
	} elsif ( $k eq "r" ) {
	    roundarrows();
	} # else k was zero which means no arrows today thank you
    }
    
    print MP " path p[],q[] ; \n" ;
    foreach $c (@color) {
	print MP " p$c := \n" ;
	print MP "  $dance{$whereis{0,$c}}..\n" ; # subpath 0
	$n = 0 ; 
	print STDERR "looping color  $c, $l = $length{0,$c} .. $length{1,$c} \n" if $verbose ;
	# potential confusion here as l runs over vertices or edges, and they differ in total number by 1
	for ( $l = $length{0,$c} ; $l <= $length{1,$c} ; $l ++ ) {
	    print MP "  ($x{$c,$l},$y{$c,$l})..\n" ;
	    print DAT "$x{$c,$l} $y{$c,$l}\n" ; 
	    $n ++ ; 
	    $lc2n{$l,$c} = $n ;
	}
	print MP "  $dance{$whereis{1,$c}} ; \n" ;
	# path is now defined
	print MP " q$c := p$c scaled $uname ;\n" ; 
	print MP "  $ropepen\n"; 
	print MP "  linecap:=rounded; \n" ;
	drawedge("(0,1) of q$c");
#	print MP " draw subpath (0,1) of q$c withcolor $colorname[$c] ;\n" ;
	$nn = $n+1 ;
	drawedge("($n,$nn) of q$c");
#	print MP " draw subpath ($n,$nn) of q$c withcolor $colorname[$c] ;\n" ;

	# we also write stuff to the DAT file in case we want a simple gnuplot picture. Here we manage the "index" integers for gnuplot.
	print DAT "\n\n" ;
	$indexo = $index ;	$index ++ ; 
    }

    for ( $ed = 1 ; $ed <= $edge ; $ed ++ ) {
	$c=$orderc[$ed] ;
	$l=$orderl[$ed] ;
	$n = $lc2n{$l,$c} ;
	print STDERR "writing edge $ed : $c, $l, $n\n" if $verbose ;
	$nn = $n-1 ;
	$nnn = $n ;
	drawedge("($nn,$nnn) of q$c");
    }

    if ($showdancers) {
	# draw all four
	for ($d=1;$d<=4;$d++) {
	    print MP "  $linepen\n"; 
	    print MP "fill (d$whois[$d] shifted ($dancer{$d} scaled $uname) ) withcolor red; \n" ;
	    print MP "draw (d$whois[$d] shifted ($dancer{$d} scaled $uname) ) withcolor black; \n" ;
	}
    }

    # close of the figure
    print MP "endfig;\n" ;
    print DAT "\n\n" ;
    $gnustring .= "plot '$datout' index $indexo w linesp 1,  '$datout' index $index w linesp 6 \n" ; 
}

sub drawedge { # usage   "($nn,$nnn) of q$c"
    local($s)=@_;
    
    if ($usewhite) {
	print MP "    linecap := butt ;\n" ;
	print MP "  $gappen\n"; 
	print MP "    undraw subpath $s ;\n" ;
	print MP "  $ropepen\n"; 
#	print MP "  linecap:=rounded; \n" ;
    }
    print MP "    draw subpath $s  withcolor $colorname[$c] ;\n" ;
    if ( $usewhitecentre[$c] ) {
	print MP "  $centrepen\n" ; 
	print MP "    undraw subpath $s ;\n" ;
    }
}





