
/* Postscript plotting routines */

#include "k3.h"
#include <stdio.h>

#define PAGEX 540       /* Page size */
#define PAGEY 720       /* Offset of lower left corner */
#define CORNX 36
#define CORNY 36
#define LINEWIDTH 0.20  /* Line width in device units */
#define BORDER 0.10     /* Default border percentage */

/* Defaults */
static double linewidth=LINEWIDTH;
double size, xmax, xmin, ymax, ymin;

/* Start plot */
void ps_open(argc,argv)
	int argc;
	char *argv[];
{
	int i;

	printf("%%!\n");
	printf("%%%% Traj Version 2.0\n");
	printf("%%%% Command line:\n");
        printf("%%%% ");
        for(i=0; i<argc; i++)
                printf("%s ",argv[i]);
	printf("\n");
}

/* Scale and box */
void ps_window(centerx, centery, radius)
	double centerx, centery;
	double radius;
{
	int marginx, marginy;
	double s, sx, sy, tx, ty;

	xmin = centerx - radius;
	xmax = centerx + radius;
	ymin = centery - radius;
	ymax = centery + radius;

/* Print header */
	sx = PAGEX/(xmax-xmin);
	sy = PAGEY/(ymax-ymin);
	s  = (sx < sy) ? sx : sy;
	marginx = (PAGEX-s*(xmax-xmin))/2;
	marginy = (PAGEY-s*(ymax-ymin))/2;

	printf("%%%% Plot of stable manifolds\n");
	printf("%%%%\n");
	printf("%%%%BoundingBox:  %d %d %d %d\n",
		CORNX+marginx,
		CORNY+marginy,
		CORNX+PAGEX-marginx,
		CORNY+PAGEY-marginy);
	printf("%d %d translate\n",
		CORNX+marginx,
		CORNY+marginy);
	printf("%.2lf setlinewidth\n",linewidth);
	printf("%%%%\n");
	printf("%%%% Change to complex coordinates\n");
	printf("%.5lf %.5lf scale\n",s,s);
	printf("currentlinewidth %.5lf div setlinewidth\n",s);
	printf("%.5lf %.5lf translate\n",-xmin,-ymin); 
	printf("%%%% Clipping box %%%%\n");
	printf(
"newpath\n %.5lf %.5lf moveto %.5lf %.5lf lineto\n %.5lf %.5lf lineto %.5lf %.5lf lineto\nclosepath clip\n",
		xmin,ymin,xmin,ymax,xmax,ymax,xmax,ymin);
	printf("/l {newpath moveto lineto stroke} def\n");
}

/* Determine if point is inside window */
int inside(p)
	point p;
{
	if (p.x < xmin) return(0);
	if (p.x > xmax) return(0);
	if (p.y < ymin) return(0);
	if (p.y > ymax) return(0);
	return(1);
}
	
void ps_line(p,q)
	point p, q;
{
	if(inside(p) || inside(q))
		printf("%.5lf %.5lf %.5lf %.5lf l\n",
		p.x,p.y,q.x,q.y);
}

void ps_close()
{
	printf("showpage\n");
}
