#!/usr/local/bin/perl
#
#	genps.pl
#
#	Generate a powersupply in Postscript.
#	This can then be used to generate an appropriate GIF for
#	inclusion.
#
#	We are working in a space of 8.5*72 x 11*72 which works
#	out to 612 x 792.  Remember that small values of Y are towards
#	the bottom of the page.
#

###
###	Constants
###
$TerminalRadius	= 5;		# Size of a terminal
$TwoPi		= 3.14159 * 2;	# A full revolution

###
###	Code
###

    print "%!\n";
    print "/Times-Roman findfont 12 scalefont setfont\n";
    print "0 0 0 setrgbcolor\n";

    text(30, 160, "9V AC");
    text(30, 140, " -or-");
    text(30, 120, "9V DC");

    $curline = 200;
    terminal(50, $curline);
    line(50 + $TerminalRadius, $curline, 200, $curline);
    hdiode(100, $curline, "1N4001");
    vcap(140, $curline, $curline - 100, "+", "2000uf", "");
    connection(140, $curline);
    box(200, $curline - 25, 275, $curline + 25);
    text(225, $curline + 35, "7805");
    text(205, $curline - 4, "IN");
    text(245, $curline - 4, "OUT");
    text(225, $curline - 15, "GND");
    ground(237, $curline - 25);
    line(275, $curline, 350 - $TerminalRadius, $curline);
    vcap(325, $curline, $curline - 50, "+", "100uf", "");
    ground(325, $curline - 50);
    connection(325, $curline);
    terminal(350, $curline);
    text(360, $curline, "+5V");

    $curline = 100;
    terminal(50, $curline);
    line(50 + $TerminalRadius, $curline, 140, $curline);
    ground(120, $curline);
    connection(120, $curline);

    print "showpage\n";


###
###	box
###
###	Draw a box
###
sub box
    {
    my($x1, $y1, $x2, $y2) = @_;

    print "newpath\n";
    print "$x1 $y1 moveto ";
    print "$x1 $y2 lineto ";
    print "$x2 $y2 lineto ";
    print "$x2 $y1 lineto ";
    print "closepath stroke\n";
    }


###
###	connection
###
###	Drawn a filled circle at a specified point.
###
sub connection
    {
    my($x, $y) = @_;
    my($rad);

    print "newpath\n";
    $rad = $TerminalRadius - 2;
    print "$x $y $rad 0 360 arc fill\n";
    }


###
###	ground
###
###	Drawn a ground terminal.
###
sub ground
    {
    my($x, $y) = @_;

    line($x, $y, $x, $y - 15);
    $y -= 15;
    line($x - 8, $y, $x + 8, $y);
    line($x - 6, $y - 3, $x + 6, $y - 3);
    line($x - 4, $y - 6, $x + 4, $y - 6);
    }


###
###	hdiode
###
###	Place a horizontal diode.
###
sub hdiode
    {
    my($x, $y, $title) = @_;
    my($length, $height);

    $length = 15;
    $height = 7;

    print "newpath\n";
    printf "%d %d moveto ", $x, $y - $height;
    printf "%d %d lineto ", $x, $y + $height;
    printf "%d %d lineto ", $x + $length, $y;
    print "closepath fill\n";

    printf "%d %d moveto ", $x + $length, $y - $height;
    printf "%d %d lineto ", $x + $length, $y + $height;
    print "stroke\n";

    text($x - 5, $y + 10, $title);
    }


###
###	line
###
###	Draw a line between two x/y points.
###
sub line
    {
    my($x1, $y1, $x2, $y2) = @_;

    print "$x1 $y1 moveto $x2 $y2 lineto stroke\n";
    }


###
###	terminal
###
###	Drawn an empty circle at a specified point.
###
sub terminal
    {
    my($x, $y) = @_;

    print "newpath\n";
    print "$x $y $TerminalRadius 0 360 arc stroke\n";
    }


###
###	text
###
###	Draw text in the page
###
sub text
    {
    my($x, $y, $text) = @_;

    print "$x $y moveto ($text) show\n";
    }


###
###	vcap
###
###	Draw a capacitor along a vertical axis.
###	Right now, $y1 should be > $y2.
###
sub vcap
    {
    my($x1, $y1, $y2, $label1, $label2, $label3) = @_;
    my($mid);

    $mid = ($y1 + $y2) / 2 + 3;
    line($x1, $y1, $x1, $mid);
    line($x1 - 8, $mid, $x1 + 8, $mid);
    $mid -= 6;
    line($x1 - 8, $mid, $x1 + 8, $mid);
    line($x1, $mid, $x1, $y2);
    text($x1 + 3, $mid + 12, $label1) if ($label1 ne "");
    text($x1 + 12, $mid + 0, $label2) if ($label2 ne "");
    text($x1 + 3, $mid - 3, $label3) if ($label3 ne "");
    }
