home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 344b.lha / plplot_v2.6 / src / plbox.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-01-27  |  11.3 KB  |  398 lines

  1. /* This draws a box around the current viewport. XOPT and YOPT are */
  2. /* character strings which define the box as follows: */
  3.  
  4. /* A: Draw axis (X is horizontal line Y=0, Y is vertical line X=0) */
  5. /* B: Draw bottom (X) or left (Y) edge of frame */
  6. /* C: Draw top (X) or right (Y) edge of frame */
  7. /* G: Draws a grid at the major tick interval */
  8. /* I: Inverts tick marks */
  9. /* L: Logarithmic axes, major ticks at decades, minor ticks at units */
  10. /* N: Write numeric label at conventional location */
  11. /* M: Write numeric label at unconventional location */
  12. /* T: Draw major tick marks */
  13. /* S: Draw minor tick marks */
  14. /* V: (for Y only) Label vertically */
  15.  
  16. /* xtick, ytick are the major tick intervals required, zero for  */
  17. /*              automatic selection */
  18. /* nxsub, nysub are the number of subtick intervals in a major tick */
  19. /*              interval */
  20.  
  21. #include "plplot.h"
  22. #include <stdio.h>
  23. #include <math.h>
  24. #ifdef PLSTDC
  25. #include <string.h>
  26. #else
  27. extern char *strcat();
  28. extern char *strcpy();
  29. #endif
  30.  
  31. #define betw(c,a,b) ((a<=c && c<=b) || (b<=c && c<=a))
  32.  
  33. static PLFLT xlog[8] = {0.301030, 0.477121, 0.602060, 0.698970,
  34.                         0.778151, 0.845098, 0.903090, 0.954243};
  35.  
  36. void plbox(xopt,xtick,nxsub,yopt,ytick,nysub)
  37. char *xopt, *yopt;
  38. PLFLT xtick, ytick;
  39. PLINT nxsub, nysub;
  40. {
  41.       static char string[40];
  42.       char strtmp[4];
  43.       PLINT lax,lbx,lcx,lgx,lix,llx,lmx,lnx,lsx,ltx;
  44.       PLINT lay,lby,lcy,lgy,liy,lly,lmy,lny,lsy,lty,lvy;
  45.       PLINT xmajor, xminor, ymajor, yminor, xmode, xprec;
  46.       PLINT ymode, yprec;
  47.       PLINT i, i1x, i2x, i3x, i4x, i1y, i2y, i3y, i4y, it0;
  48.       PLINT nxsub1, nysub1;
  49.       PLINT lxmin, lxmax, lymin, lymax;
  50.       PLINT pxmin, pxmax, pymin, pymax;
  51.       PLINT vppxmi, vppxma, vppymi, vppyma;
  52.       PLINT level;
  53.       PLFLT xpmm, ypmm, defmaj, defmin, htmaj, htmin;
  54.       PLFLT xtick1, ytick1, vpwxmi, vpwxma, vpwymi, vpwyma;
  55.       PLFLT pos, tn, tp, temp;
  56.  
  57.       glev(&level);
  58.       if (level<3) plexit("Please set up window before calling plbox.");
  59.  
  60.       /* Open  the clip limits to the subpage limits */
  61.       gclp(&lxmin,&lxmax,&lymin,&lymax);
  62.       gphy(&pxmin,&pxmax,&pymin,&pymax);
  63.       sclp(pxmin,pxmax,pymin,pymax);
  64.  
  65.       gvpp(&vppxmi,&vppxma,&vppymi,&vppyma);
  66.  
  67.       /* Tick and subtick sizes in device coords */
  68.       gpixmm(&xpmm,&ypmm);
  69.       gmaj(&defmaj,&htmaj);
  70.       gmin(&defmin,&htmin);
  71.  
  72.       xmajor=max(round(htmaj*ypmm),1);
  73.       ymajor=max(round(htmaj*xpmm),1);
  74.       xminor=max(round(htmin*ypmm),1);
  75.       yminor=max(round(htmin*xpmm),1);
  76.  
  77.       xtick1=xtick;
  78.       nxsub1=nxsub;
  79.       ytick1=ytick;
  80.       nysub1=nysub;
  81.  
  82.       lax=stsearch(xopt,'a');
  83.       lbx=stsearch(xopt,'b');
  84.       lcx=stsearch(xopt,'c');
  85.       lgx=stsearch(xopt,'g');
  86.       lix=stsearch(xopt,'i');
  87.       llx=stsearch(xopt,'l');
  88.       lmx=stsearch(xopt,'m');
  89.       lnx=stsearch(xopt,'n');
  90.       lsx=stsearch(xopt,'s');
  91.       ltx=stsearch(xopt,'t');
  92.  
  93.       lay=stsearch(yopt,'a');
  94.       lby=stsearch(yopt,'b');
  95.       lcy=stsearch(yopt,'c');
  96.       lgy=stsearch(yopt,'g');
  97.       liy=stsearch(yopt,'i');
  98.       lly=stsearch(yopt,'l');
  99.       lmy=stsearch(yopt,'m');
  100.       lny=stsearch(yopt,'n');
  101.       lsy=stsearch(yopt,'s');
  102.       lty=stsearch(yopt,'t');
  103.       lvy=stsearch(yopt,'v');
  104.  
  105.       gvpw(&vpwxmi,&vpwxma,&vpwymi,&vpwyma);
  106.  
  107.       lax=lax && (vpwymi*vpwyma<0.0) && !llx;
  108.       lay=lay && (vpwxmi*vpwxma<0.0) && !lly;
  109.       if (llx) xtick1=1.0;
  110.       if (lly) ytick1=1.0;
  111.       if (ltx || lgx)
  112.           pldtik(vpwxmi,vpwxma,&xtick1,&nxsub1,&xmode,&xprec);
  113.       if (lty || lgy)
  114.           pldtik(vpwymi,vpwyma,&ytick1,&nysub1,&ymode,&yprec);
  115.  
  116.       /* Set up tick variables */
  117.       if (lix) {
  118.         i1x=xminor;
  119.         i2x=0;
  120.         i3x=xmajor;
  121.         i4x=0;
  122.       }
  123.       else {
  124.         i1x=0;
  125.         i2x=xminor;
  126.         i3x=0;
  127.         i4x=xmajor;
  128.       }
  129.  
  130.       if (liy) {
  131.         i1y=yminor;
  132.         i2y=0;
  133.         i3y=ymajor;
  134.         i4y=0;
  135.       }
  136.       else {
  137.         i1y=0;
  138.         i2y=yminor;
  139.         i3y=0;
  140.         i4y=ymajor;
  141.       }
  142.  
  143.  
  144.       /* Draw the bottom edge of the box */
  145.       if (lbx) {
  146.         movphy(vppxmi,vppymi);
  147.         if (ltx) {
  148.           tp=xtick1*floor(vpwxmi/xtick1);
  149.           for(;;) {
  150.              tn=tp+xtick1;
  151.              if (lsx) {
  152.                if (llx) {
  153.                  for(i=0; i<=7;i++){
  154.                    temp=tp+xlog[i];
  155.                    if (betw(temp,vpwxmi,vpwxma))
  156.                         plxtik(wcpcx(temp),vppymi,i1x,i2x);
  157.                  }
  158.                }
  159.                else {
  160.                  for ( i=1;i<=nxsub1-1;i++) {
  161.                    temp=tp+i*(tn-tp)/nxsub1;
  162.                    if (betw(temp,vpwxmi,vpwxma))
  163.                         plxtik(wcpcx(temp),vppymi,i1x,i2x);
  164.                  }
  165.                }
  166.              }
  167.              if (!betw(tn,vpwxmi,vpwxma))
  168.                break;
  169.              plxtik(wcpcx(tn),vppymi,i3x,i4x);
  170.              tp=tn;
  171.           }
  172.         }
  173.         draphy(vppxma,vppymi);
  174.       }
  175.  
  176.       /* Draw right-hand edge of box */
  177.       if (lcy) {
  178.         movphy(vppxma,vppymi);
  179.         if (lty) {
  180.           tp=ytick1*floor(vpwymi/ytick1);
  181.           for(;;) {
  182.              tn=tp+ytick1;
  183.              if (lsy) {
  184.                if (lly) {
  185.                  for(i=0;i<=7;i++) {
  186.                    temp=tp+xlog[i];
  187.                    if (betw(temp,vpwymi,vpwyma))
  188.                         plytik(vppxma,wcpcy(temp),i2y,i1y);
  189.                  }
  190.                }
  191.                else {
  192.                  for(i=1;i<=nysub1-1;i++) {
  193.                    temp=tp+i*(tn-tp)/nysub1;
  194.                    if (betw(temp,vpwymi,vpwyma))
  195.                         plytik(vppxma,wcpcy(temp),i2y,i1y);
  196.                  }
  197.                }
  198.              }
  199.              if (!betw(tn,vpwymi,vpwyma))
  200.                 break;
  201.              plytik(vppxma,wcpcy(tn),i4y,i3y);
  202.              tp=tn;
  203.           }
  204.         }
  205.         draphy(vppxma,vppyma);
  206.       }
  207.  
  208.       /* Draw the top edge of the box */
  209.       if (lcx) {
  210.         movphy(vppxma,vppyma);
  211.         if (ltx) {
  212.           tp=xtick1*(floor(vpwxma/xtick1)+1);
  213.           for(;;) {
  214.              tn=tp-xtick1;
  215.              if (lsx) {
  216.                if (llx) {
  217.                  for(i=7;i>=0;i--) {
  218.                    temp=tn+xlog[i];
  219.                    if (betw(temp,vpwxmi,vpwxma))
  220.                         plxtik(wcpcx(temp),vppyma,i2x,i1x);
  221.                  }
  222.                }
  223.                else {
  224.                  for(i=nxsub1-1;i>=1;i--) {
  225.                    temp=tn+i*(tp-tn)/nxsub1;
  226.                    if (betw(temp,vpwxmi,vpwxma))
  227.                         plxtik(wcpcx(temp),vppyma,i2x,i1x);
  228.                  }
  229.                }
  230.              }
  231.              if (!betw(tn,vpwxmi,vpwxma))
  232.                break;
  233.              plxtik(wcpcx(tn),vppyma,i4x,i3x);
  234.              tp=tn;
  235.           }
  236.         }
  237.         draphy(vppxmi,vppyma);
  238.       }
  239.  
  240.       /* Draw left-hand edge of box */
  241.       if (lby) {
  242.         movphy(vppxmi,vppyma);
  243.         if (lty) {
  244.           tp=ytick1*(floor(vpwyma/ytick1)+1);
  245.           for(;;) {
  246.              tn=tp-ytick1;
  247.              if (lsy) {
  248.                if (lly) {
  249.                  for(i=7;i>=0;i--) {
  250.                    temp=tn+xlog[i];
  251.                    if (betw(temp,vpwymi,vpwyma))
  252.                         plytik(vppxmi,wcpcy(temp),i1y,i2y);
  253.                  }
  254.                }
  255.                else {
  256.                  for(i=nysub1-1;i>=1;i--) {
  257.                    temp=tn+i*(tp-tn)/nysub1;
  258.                    if (betw(temp,vpwymi,vpwyma))
  259.                         plytik(vppxmi,wcpcy(temp),i1y,i2y);
  260.                  }
  261.                }
  262.              }
  263.              if (!betw(tn,vpwymi,vpwyma))
  264.                break;
  265.              plytik(vppxmi,wcpcy(tn),i3y,i4y);
  266.              tp=tn;
  267.           }
  268.         }
  269.         draphy(vppxmi,vppymi);
  270.       }
  271.  
  272.       /* Draw the horizontal axis */
  273.       if (lax) {
  274.         it0=wcpcy((PLFLT)0.0);
  275.         movphy(vppxmi,it0);
  276.         if (ltx) {
  277.           tp=xtick1*floor(vpwxmi/xtick1);
  278.           for(;;) {
  279.              tn=tp+xtick1;
  280.              if (lsx) {
  281.                if (llx) {
  282.                  for(i=0;i<=7;i++) {
  283.                    temp=tp+xlog[i];
  284.                    if (betw(temp,vpwxmi,vpwxma))
  285.                         plxtik(wcpcx(temp),it0,xminor,xminor);
  286.                  }
  287.                }
  288.                else {
  289.                  for(i=1;i<=nxsub1-1;i++) {
  290.                    temp=tp+i*(tn-tp)/nxsub1;
  291.                    if (betw(temp,vpwxmi,vpwxma))
  292.                         plxtik(wcpcx(temp),it0,xminor,xminor);
  293.                  }
  294.                }
  295.              }
  296.              if (!betw(tn,vpwxmi,vpwxma))
  297.                break;
  298.              plxtik(wcpcx(tn),it0,xmajor,xmajor);
  299.              tp=tn;
  300.           }
  301.         }
  302.         draphy(vppxma,it0);
  303.       }
  304.  
  305.       /* Draw the vertical axis */
  306.       if (lay) {
  307.         it0=wcpcx((PLFLT)0.0);
  308.         movphy(it0,vppymi);
  309.         if (lty) {
  310.           tp=ytick1*floor(vpwymi/ytick1);
  311.           for(;;) {
  312.              tn=tp+ytick1;
  313.              if (lsy) {
  314.                if (lly) {
  315.                  for(i=0;i<=7;i++) {
  316.                    temp=tp+xlog[i];
  317.                    if (betw(temp,vpwymi,vpwyma))
  318.                       plytik(it0,wcpcy(temp),yminor,yminor);
  319.                  }
  320.                }
  321.                else {
  322.                  for(i=1;i<=nysub1-1;i++) {
  323.                    temp=tp+i*(tn-tp)/nysub1;
  324.                    if (betw(temp,vpwymi,vpwyma))
  325.                         plytik(it0,wcpcy(temp),yminor,yminor);
  326.                  }
  327.                }
  328.              }
  329.              if (!betw(tn,vpwymi,vpwyma))
  330.                break;
  331.              plytik(it0,wcpcy(tn),ymajor,ymajor);
  332.              tp=tn;
  333.           }
  334.         }
  335.         draphy(it0,vppyma);
  336.       }
  337.  
  338.       /* Draw grid in x direction */
  339.       if (lgx) {
  340.         tp=xtick1*(1.+floor(vpwxmi/xtick1+.5));
  341.         for(tn=tp; betw(tn+xtick1/2.,vpwxmi,vpwxma); tn+=xtick1)
  342.            pljoin(tn,vpwymi,tn,vpwyma);
  343.       }
  344.  
  345.       /* Draw grid in y direction */
  346.       if (lgy) {
  347.         tp=ytick1*(1.+floor(vpwymi/ytick1+.5));
  348.         for(tn=tp; betw(tn+ytick1/2.,vpwymi,vpwyma); tn+=ytick1)
  349.            pljoin(vpwxmi,tn,vpwxma,tn);
  350.       }
  351.  
  352.       /* Write horizontal label(s) */
  353.       if ((lmx || lnx) && ltx) {
  354.         tp=xtick1*(1.+floor(vpwxmi/xtick1));
  355.         for(tn=tp; betw(tn,vpwxmi,vpwxma); tn+=xtick1) {
  356.           if (!llx)
  357.             plform(tn,xmode,xprec,string);
  358.           else {
  359.             sprintf(strtmp,"%-d",round(tn));
  360.             strcpy(string,"10#u");
  361.             strcat(string,strtmp);
  362.           }
  363.           pos=(tn-vpwxmi)/(vpwxma-vpwxmi);
  364.           if (lnx) plmtex("b",(PLFLT)1.5,pos,(PLFLT)0.5,string);
  365.           if (lmx) plmtex("t",(PLFLT)1.5,pos,(PLFLT)0.5,string);
  366.         }
  367.       }
  368.  
  369.       /* Write vertical label(s) */
  370.       if ((lmy || lny) && lty) {
  371.         tp=ytick1*(1.+floor(vpwymi/ytick1));
  372.         for(tn=tp; betw(tn,vpwymi,vpwyma); tn+=ytick1) {
  373.           if (!lly)
  374.             plform(tn,ymode,yprec,string);
  375.           else {
  376.             sprintf(strtmp,"%-d",round(tn));
  377.             strcpy(string,"10#u");
  378.             strcat(string,strtmp);
  379.           }
  380.           pos=(tn-vpwymi)/(vpwyma-vpwymi);
  381.           if (lny) {
  382.             if (lvy)
  383.               plmtex("lv",(PLFLT)0.5,pos,(PLFLT)1.0,string);
  384.             else
  385.               plmtex("l",(PLFLT)1.5,pos,(PLFLT)0.5,string);
  386.           }
  387.           if (lmy) {
  388.             if (lvy)
  389.               plmtex("rv",(PLFLT)0.5,pos,(PLFLT)0.0,string);
  390.             else
  391.               plmtex("r",(PLFLT)1.5,pos,(PLFLT)0.5,string);
  392.           }
  393.         }
  394.       }
  395.       /* Restore the clip limits to viewport edge */
  396.       sclp(lxmin,lxmax,lymin,lymax);
  397. }
  398.