#!/afs/athena/contrib/perl/p

srand;

&macros;
&place_people_1();
&make_connections_rand();
&draw_people();
print("showpage\n");

sub place_people_1 {
    for $xq (1..7) {
	for $yq (1..10){
	    $r = rand(10);
	    if($r<1.2){
		$nmen++;
		$xcor{"man$nmen"} = $xq;
		$ycor{"man$nmen"} = $yq;
	    }
	    elsif($r<2.4){
		$nwomen++;
		$xcor{"wom$nwomen"} = $xq;
		$ycor{"wom$nwomen"} = $yq;
	    }
	}
    }
}

sub place_people_rand {

    $nmen = rand(10)+4;
    $nwomen = rand(10)+4;

    for $man (1..$nmen){
	$xcor{"man$man"} = rand(8)+.25;
	$ycor{"man$man"} = rand(10.5)+.25;    
    }
    
    for $wom (1..$nwomen){
	$xcor{"wom$wom"} = rand(8.5);
	$ycor{"wom$wom"} = rand(11);   
    }
}

sub make_connections_rand {
    for $man (1..$nmen){
	for $wom (1..$nwomen){
	    if(rand(10) <= 3){
		&connect($man, $wom);
	    }
	}
    }
}

sub draw_people {
    for $man (1..$nmen){
	&drawman($xcor{"man$man"}, $ycor{"man$man"});
    }
    for $wom (1..$nwomen){
	&drawwoman($xcor{"wom$wom"}, $ycor{"wom$wom"});
    }
}

sub connect {
    local($m, $w) = @_;
    
    $x1 = $xcor{"man$m"}; $y1 = $ycor{"man$m"};
    $x2 = $xcor{"wom$w"}; $y2 = $ycor{"wom$w"};
    print("$x1 inch $y1 inch $x2 inch $y2 inch connect\n");
}

sub drawman {
    local($x, $y) = @_;

    print("$x inch $y inch square\n");
}

sub drawwoman {
    local($x, $y) = @_;

    print("$x inch $y inch circle\n");
}


sub macros{
    $macros =<<_MACROS_;
%!PS-Adobe-1.0
/inch {72 mul} def
1 setlinewidth
/radius 10 def
/connect {/y2 exch def /x2 exch def /y1 exch def /x1 exch def
x1 y1 moveto x2 y2 lineto stroke
} def
/circle {/y exch def /x exch def
x radius add y moveto
x y radius 0 360 arc gsave 1 setgray fill grestore stroke} def
/square {/y exch def /x exch def
x radius sub y radius sub moveto
radius 2 mul 0 rlineto 0 radius 2 mul rlineto radius -2 mul 0 rlineto
closepath gsave 1 setgray fill grestore stroke} def
_MACROS_

    print $macros;
}
