home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / plotting / contour / contour.lha / Contour / plotps.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-08-28  |  10.9 KB  |  358 lines

  1. /*** plotps.c - plot the contours in Postscript ***/
  2.  
  3. #include <stdio.h>
  4. #include <strings.h>
  5. #include <math.h>
  6. #include "common.h"
  7. #include "contour.h"
  8. #include "plot.h"
  9.  
  10. #define PX_DIM   600
  11. #define PY_DIM   800
  12. #define PBDR_DIM 100
  13. #define PXL_DIM  800
  14. #define PYL_DIM  600
  15.  
  16. double  Pxmin,Pymin,Pxmax,Pymax;
  17.  
  18. /* Output some postscript subroutines */
  19. initps(ips)
  20. FILE *ips;
  21. {
  22.    extern double scale;
  23.    char   time[MAXCHAR];
  24.  
  25.    /* begin postscript output */
  26.    fprintf(ips,"%c!\n",'%');
  27.  
  28.    if (landscape) fprintf(ips,"-90 rotate -790 0 translate\n");
  29.    /*
  30.    if (scale < 1.0) fprintf(ips,"%6.3f %6.3f scale\n",scale,scale);
  31.    scale = 1.0;
  32.    */
  33.  
  34.    fprintf(ips,"/defFontSizeTop %6.2f def\n",25*scale);
  35.    fprintf(ips,"/defFontSizeSide %6.2f def\n",20*scale);
  36.    fprintf(ips,"/defFontSizeAxes %6.2f def\n",15*scale);
  37.    fprintf(ips,"/defFontSizeMkrs %6.2f def\n",12*scale);
  38.    fprintf(ips,"/defFontSizeLbls %6.2f def\n",8*scale);
  39.  
  40.    fprintf(ips,"/UPJ  {} def\n");
  41.    fprintf(ips,"/MIJT  {0 defFontSizeTop -2 div rmoveto} def\n");
  42.    fprintf(ips,"/BOJT  {0 defFontSizeTop neg rmoveto} def\n");
  43.    fprintf(ips,"/MIJS  {0 defFontSizeSide -2 div rmoveto} def\n");
  44.    fprintf(ips,"/BOJS  {0 defFontSizeSide neg rmoveto} def\n");
  45.    fprintf(ips,"/MIJA  {0 defFontSizeAxes -2 div rmoveto} def\n");
  46.    fprintf(ips,"/BOJA  {0 defFontSizeAxes neg rmoveto} def\n");
  47.    fprintf(ips,"/MIJM  {0 defFontSizeLbls -2 div rmoveto} def\n");
  48.    fprintf(ips,"/BOJM  {0 defFontSizeLbls neg rmoveto} def\n");
  49.    fprintf(ips,"/MIJL  {0 defFontSizeMkrs -3 div rmoveto} def\n");
  50.    fprintf(ips,"/BOJL  {0 defFontSizeMkrs neg rmoveto} def\n");
  51.  
  52.    fprintf(ips,"/LEJ  {} def\n");
  53.    fprintf(ips,"/CEJ  {dup stringwidth pop -2 div 0 rmoveto} def\n");
  54.    fprintf(ips,"/RIJ  {dup stringwidth pop neg 0 rmoveto} def\n");
  55.  
  56.    /* print the time */
  57.    get_localtime(time);
  58.    fprintf(ips,"/Times-Bold findfont\n");
  59.    fprintf(ips,"10 scalefont setfont\n");
  60.    if (landscape)
  61.       fprintf(ips,"%6.2f %6.2f moveto ",30.0,570.0);
  62.    else 
  63.       fprintf(ips,"%6.2f %6.2f moveto ",30.0,750.0);
  64.    fprintf(ips,"(%s) show\n",time);
  65. }
  66.  
  67. /* draw the axes */
  68. axesps(ips)
  69. FILE *ips;
  70. {
  71.     char   *sprintf();
  72.     extern char    xlabel[MAXCHAR];
  73.     extern char    ylabel[MAXCHAR];
  74.     extern char    toplabel[MAXCHAR];
  75.     extern int     grid,equalscale,xticks,yticks;
  76.     extern double  scale;
  77.     extern double  xmin, xmax, ymin, ymax;
  78.  
  79.     double xdx,ydy;
  80.     double xintv,yintv,xtmp,ytmp,xwid,ywid,ratio;
  81.     double vallbl;
  82.     int    i;
  83.     char   label[10];
  84.  
  85.     /* Initialize */
  86.     xdx = (xmax-xmin)/xticks;
  87.     ydy = (ymax-ymin)/yticks;
  88.  
  89.     Pxmin = PBDR_DIM;
  90.     Pymin = PBDR_DIM;
  91.     xwid  = PX_DIM - 2*PBDR_DIM;
  92.     ywid  = PY_DIM - 2*PBDR_DIM;
  93.     if (landscape) {
  94.        xwid  = PXL_DIM - 2*PBDR_DIM;      /* 600 */
  95.        ywid  = PYL_DIM - 2*PBDR_DIM;      /* 400 */
  96.     }
  97.     Pxmax = Pxmin + scale*xwid;
  98.     Pymax = Pymin + scale*ywid; 
  99.     if (equalscale == ON) {
  100.        /* max xwid = 400, max ywid = 600 */
  101.        ratio = (ymax-ymin)/(xmax-xmin);
  102.        if (ratio <= (ywid/xwid)) {
  103.           Pxmax = Pxmin + scale*xwid;
  104.           Pymax = Pymin + ratio*scale*xwid;
  105.       } else {
  106.           Pymax = Pymin + scale*ywid; 
  107.           Pxmax = Pxmin + scale*ywid/ratio;
  108.           }
  109.        }
  110.     xintv = (Pxmax-Pxmin)/xticks;
  111.     yintv = (Pymax-Pymin)/yticks;
  112.  
  113.     /* define the axes */
  114.     fprintf(ips,"/rectanglepath {\n");
  115.     fprintf(ips,"newpath\n  %6.2f %6.2f moveto\n",Pxmin,Pymin);
  116.     fprintf(ips,"  0.0 %6.2f rlineto\n",Pymax-Pymin);
  117.     fprintf(ips,"  %6.2f 0.0 rlineto\n",Pxmax-Pxmin);
  118.     fprintf(ips,"  0.0 %6.2f rlineto\n",Pymin-Pymax);
  119.     fprintf(ips,"closepath } def\n");
  120.     fprintf(ips,"%6.2f setlinewidth\nrectanglepath stroke\n",2*scale);
  121.  
  122.     /* Top Label */
  123.     fprintf(ips,"/Times-Bold findfont\n");
  124.     fprintf(ips,"defFontSizeTop scalefont setfont\n");
  125.     fprintf(ips,"  %6.2f %6.2f moveto ",0.5*(Pxmax+Pxmin),Pymax+25*scale);
  126.     fprintf(ips,"(%s) UPJ CEJ show\n",toplabel);
  127.  
  128.     /* X-axis Label */
  129.     fprintf(ips,"/Times-Bold findfont\n");
  130.     fprintf(ips,"defFontSizeSide scalefont setfont\n");
  131.     fprintf(ips,"  %6.2f %6.2f moveto ",0.5*(Pxmax+Pxmin),Pymin-20*scale);
  132.     fprintf(ips,"(%s) BOJS CEJ show\n",xlabel);
  133.  
  134.     /* Y-axis Label */
  135.     fprintf(ips,"/Times-Bold findfont\n");
  136.     fprintf(ips,"defFontSizeSide scalefont setfont\n");
  137.     fprintf(ips,"/Ylabel {");
  138.     fprintf(ips,"%6.2f %6.2f moveto ",0.5*(Pymax+Pymin),50*scale-Pxmin);
  139.     fprintf(ips,"(%s) UPJ CEJ show ",ylabel);
  140.     fprintf(ips,"} def\n  90 rotate\n  Ylabel\n  -90 rotate\n"); 
  141.  
  142.     /* side labels */
  143.     fprintf(ips,"/Times-Roman findfont\n");
  144.     fprintf(ips,"defFontSizeMkrs scalefont setfont\n");
  145.     sprintf(label,"%s","MINIMUM");
  146.     fprintf(ips,"  %6.2f %6.2f moveto",Pxmax+40*scale,Pymax-20*scale);
  147.     fprintf(ips,"  (%s) MIJM CEJ show\n",label);
  148.     sprintf(label,"%.2f",zmin);
  149.     fprintf(ips,"  %6.2f %6.2f moveto",Pxmax+40*scale,Pymax-40*scale);
  150.     fprintf(ips,"  (%s) MIJM CEJ show\n",label);
  151.     sprintf(label,"%s","MAXIMUM");
  152.     fprintf(ips,"  %6.2f %6.2f moveto",Pxmax+40*scale,Pymin+40*scale);
  153.     fprintf(ips,"  (%s) MIJM CEJ show\n",label);
  154.     sprintf(label,"%.2f",zmax);
  155.     fprintf(ips,"  %6.2f %6.2f moveto",Pxmax+40*scale,Pymin+20*scale);
  156.     fprintf(ips,"  (%s) MIJM CEJ show\n",label);
  157.  
  158.     /* Tick Marks on the X-axis */
  159.     fprintf(ips,"/Times-Roman findfont\n");
  160.     fprintf(ips,"defFontSizeAxes scalefont setfont\n");
  161.     for (i=0; i<=xticks; i++) {
  162.        xtmp = Pxmin + xintv*i;
  163.        ytmp = Pymin;
  164.        fprintf(ips,"newpath %6.2f %6.2f moveto\n",xtmp,ytmp);
  165.        fprintf(ips,"  0 %6.2f rlineto stroke\n",3*scale);
  166.  
  167.        vallbl = xmin + i*xdx;
  168.        sprintf(label,"%.2g",vallbl);
  169.        fprintf(ips,"  %6.2f %6.2f moveto",xtmp,ytmp);
  170.        fprintf(ips,"  (%s) BOJA CEJ show\n",label);
  171.  
  172.        ytmp = Pymax;
  173.        fprintf(ips,"newpath %6.2f %6.2f moveto\n",xtmp,ytmp);
  174.        fprintf(ips,"  0 %6.2f rlineto stroke\n",-3*scale);
  175.     }
  176.  
  177.     /* Tick Marks on the Y-axis */
  178.     for (i=0; i<=yticks; i++) {
  179.        ytmp = Pymin + yintv*i;
  180.        xtmp = Pxmin;
  181.        fprintf(ips,"newpath %6.2f %6.2f moveto\n",xtmp,ytmp);
  182.        fprintf(ips,"  %6.2f 0 rlineto stroke\n",3*scale);
  183.  
  184.        vallbl = ymin + i*ydy;
  185.        sprintf(label,"%.2g",vallbl);
  186.        fprintf(ips,"  %6.2f %6.2f moveto",xtmp-5*scale,ytmp+3*scale);
  187.        fprintf(ips,"  (%s) MIJA RIJ show\n",label);
  188.  
  189.        xtmp = Pxmax;
  190.        fprintf(ips,"newpath %6.2f %6.2f moveto\n",xtmp,ytmp);
  191.        fprintf(ips,"  %6.2f 0 rlineto stroke\n",-3*scale);
  192.     }
  193.  
  194.     /* draw the grid */
  195.     for (i=1; i<xticks; i++) {
  196.        xtmp = Pxmin + xintv*i;
  197.        ytmp = Pymax - 3*scale;
  198.        if (grid == ON) {
  199.           fprintf(ips,"[1 4] 0 setdash\n");
  200.           fprintf(ips,"%6.2f setlinewidth 0.5 setgray\n",0.3*scale);
  201.           fprintf(ips,"newpath %6.2f %6.2f moveto ",xtmp,ytmp);
  202.           fprintf(ips,"0 %6.2f rlineto stroke\n",Pymin-Pymax+6*scale);
  203.           fprintf(ips,"%6.2f setlinewidth 0.0 setgray\n",2.0*scale);
  204.           linetypps(ips,0);
  205.        }
  206.     }
  207.     for (i=1; i<yticks; i++) {
  208.        ytmp = Pymin + yintv*i;
  209.        xtmp = Pxmax - 3*scale;
  210.        if (grid == ON) {
  211.           fprintf(ips,"[1 4] 0 setdash\n");
  212.           fprintf(ips,"%6.2f setlinewidth 0.5 setgray\n",0.3*scale);
  213.           fprintf(ips,"newpath %6.2f %6.2f moveto ",xtmp,ytmp);
  214.           fprintf(ips,"%6.2f 0 rlineto stroke\n",Pxmin-Pxmax+6*scale);
  215.           fprintf(ips,"%6.2f setlinewidth 0.0 setgray\n",2.0*scale);
  216.           linetypps(ips,0);
  217.        }
  218.     }
  219.  
  220.     fprintf(ips,"%6.2f setlinewidth\n",0.5*scale);
  221.     fprintf(ips,"rectanglepath clip\n");
  222. }
  223.  
  224. /* draw the plot */
  225. plotps(ips)
  226. FILE *ips;
  227. {
  228.    int    set_label_pt();
  229.    extern int     linetypes,contlabel;
  230.    extern double  xmin, xmax, ymin, ymax;
  231.    extern plotptr plot_listhead;
  232.  
  233.    nodeptr N;
  234.    plotptr P;
  235.    double  x,y,xn, xm, yn, ym, idx, jdy, oldlevel, angle;
  236.    int     linepat, npts, j, m, n, kl=1;
  237.  
  238.    idx = (Pxmax-Pxmin)/(xmax-xmin);
  239.    jdy = (Pymax-Pymin)/(ymax-ymin);
  240.  
  241.    if (plot_listhead!=NULL) oldlevel = plot_listhead->level;
  242.    for (P=plot_listhead; P!=NULL; P=P->next) {
  243.       /* first count the number of points in the plot */
  244.       npts=0;
  245.       for (N=P->nodehead; N!=NULL; N=N->next) npts++;
  246.       m = set_label_pt(npts);
  247.       /*
  248.       m = (int)(0.45*npts)-1;
  249.       */
  250.       n = m+1;
  251.  
  252.       if (npts>0) {
  253.          /* plot the points */
  254.          if (oldlevel != P->level) kl++;
  255.          linepat = linetypps(ips,kl);
  256.          N = P->nodehead;
  257.          x = (N->pt.x-xmin)*idx + Pxmin;
  258.          y = (N->pt.y-ymin)*jdy + Pymin;
  259.          fprintf(ips,"newpath\n  %8.2f %8.2f moveto\n",x,y);
  260.          j = 1;
  261.          N = N->next;
  262.          for ( ; N!=NULL; N=N->next) {
  263.             x = (N->pt.x-xmin)*idx + Pxmin;
  264.             y = (N->pt.y-ymin)*jdy + Pymin;
  265.             fprintf(ips,"  %8.2f %8.2f lineto\n",x,y);
  266.             if (j==m) {xm = x; ym = y;}
  267.             if (j==n) {xn = x; yn = y;}
  268.             j++;
  269.          } 
  270.          fprintf(ips,"stroke\n");
  271.  
  272.          /* now put on the labels */
  273.          if (contlabel && (linepat==0 || linepat==1) && (m>=0 && n<npts)) {
  274.             if (fabs(xm-xn)<SMALL)
  275.                angle = -90.0;
  276.             else
  277.                angle = 360.0/(2*3.14159)*atan((yn-ym)/(xn-xm));
  278.             labelps(ips,xm,ym,angle,P->level);
  279.          }
  280.       }
  281.       oldlevel = P->level;
  282.    }
  283. }
  284.  
  285. /* return a linetype */
  286. int linetypps(ips,line)
  287. FILE *ips;
  288. int line; 
  289. {
  290.    extern int linetypes;
  291.    int    linepat;
  292.  
  293.    if      (linetypes <= 2) line = line % linetypes;
  294.    else if (linetypes == 3) line = line % 4;
  295.  
  296.    switch (line) {
  297.    case 0 : fprintf(ips,"[] 0 setdash\n");    linepat = 0; break;
  298.    case 1 : fprintf(ips,"[1 2] 0 setdash\n"); linepat = 2; break;
  299.    case 2 : fprintf(ips,"[6 6] 0 setdash\n"); linepat = 1; break;
  300.    case 3 : fprintf(ips,"[1 2] 0 setdash\n"); linepat = 2; break;
  301.    default: fprintf(ips,"[] 0 setdash\n");    linepat = 0; break;
  302.    }
  303.    return (linepat);
  304. }
  305.  
  306. /* paste on labels */
  307. labelps(ips,xn,yn,angle,level)
  308. FILE *ips;
  309. double xn,yn,angle,level;
  310. {
  311.    char   *sprintf();
  312.    extern double scale;
  313.    char text[100];
  314.  
  315.    sprintf(text,"%.2g",level);
  316.    fprintf(ips,"/Times-Roman findfont\n");
  317.    fprintf(ips,"defFontSizeLbls scalefont setfont\n");
  318.    fprintf(ips,"/label {");
  319.    fprintf(ips,"newpath\n ");
  320.    fprintf(ips,"%6.2f %6.2f moveto\n",-8*scale,-4*scale);
  321.    fprintf(ips,"0     %6.2f rlineto\n",8*scale);
  322.    fprintf(ips,"%6.2f 0     rlineto\n",16*scale);
  323.    fprintf(ips,"0     %6.2f rlineto\n",-8*scale);
  324.    fprintf(ips,"closepath\n 1.0 setgray \n fill \n 0 setgray\n");
  325.    fprintf(ips,"0 0 moveto\n");
  326.    fprintf(ips,"(%s) MIJL CEJ show } def\n",text);
  327.    fprintf(ips,"%6.2f %6.2f translate\n",xn,yn);
  328.    fprintf(ips,"%6.2f rotate\nlabel\n  %6.2f rotate\n",angle,-angle);
  329.    fprintf(ips,"%6.2f %6.2f translate\n",-xn,-yn);
  330. }
  331.  
  332. int set_label_pt(npts)
  333. int npts;
  334. {
  335.    int rand();
  336.    int i= -1,m;
  337.    double mult;
  338.  
  339.    if (npts < 20)
  340.       m = (int)(0.45*npts)-1;
  341.    else {
  342.       i = rand() % 10;
  343.       mult = 0.1*i + 0.05;
  344.       m = 10 + (int)(mult*(npts-20));
  345.    }
  346.    return(m);
  347. }
  348.  
  349. /* End procedure */
  350. endgrps(ips)
  351. FILE *ips;
  352. {
  353.     fprintf(ips,"showpage\n");
  354.  
  355.     /* Close the file */
  356.     fclose(ips);
  357. }
  358.