home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / alib / d3xx / d306 / rexxplplot.lha / RexxPlPlot / src / src.zoo / plbox.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-01-06  |  11.6 KB  |  432 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. #ifndef AZTEC_C
  25. #include <string.h>
  26. #endif
  27.  
  28. #define betw(c,a,b) ((a<=c && c<=b) || (b<=c && c<=a))
  29.  
  30. static float xlog[8] = {0.301030, 0.477121, 0.602060, 0.698970,
  31.                         0.778151, 0.845098, 0.903090, 0.954243};
  32.  
  33. void plbox(xopt,xtick,nxsub,yopt,ytick,nysub)
  34. char xopt[],yopt[];
  35. float xtick, ytick;
  36. int nxsub, nysub;
  37. {
  38.       char string[40];
  39.       char strtmp[4];
  40.       int lax,lbx,lcx,lgx,lix,llx,lmx,lnx,lsx,ltx;
  41.       int lay,lby,lcy,lgy,liy,lly,lmy,lny,lsy,lty,lvy;
  42.       int xmajor, xminor, ymajor, yminor, xmode, xprec;
  43.       int ymode, yprec;
  44.       int i, i1x, i2x, i3x, i4x, i1y, i2y, i3y, i4y, it0;
  45.       int nxsub1, nysub1;
  46.       int lxmin, lxmax, lymin, lymax;
  47.       int pxmin, pxmax, pymin, pymax;
  48.       int vppxmi, vppxma, vppymi, vppyma;
  49.       int level;
  50.       float xpmm, ypmm, defmaj, defmin, htmaj, htmin;
  51.       float xtick1, ytick1, vpwxmi, vpwxma, vpwymi, vpwyma;
  52.       float pos, tn, tp, temp;
  53.  
  54.       glev(&level);
  55.       if (level<3) fatal("Please set up window before calling PLBOX.");
  56.  
  57. /* Open  the clip limits to the subpage limits */
  58.  
  59.       gclp(&lxmin,&lxmax,&lymin,&lymax);
  60.       gphy(&pxmin,&pxmax,&pymin,&pymax);
  61.       sclp(pxmin,pxmax,pymin,pymax);
  62.  
  63.       gvpp(&vppxmi,&vppxma,&vppymi,&vppyma);
  64.  
  65. /* Tick and subtick sizes in device coords */
  66.  
  67.       gpixmm(&xpmm,&ypmm);
  68.       gmaj(&defmaj,&htmaj);
  69.       gmin(&defmin,&htmin);
  70.       
  71.       xmajor=max(round(htmaj*ypmm),1);
  72.       ymajor=max(round(htmaj*xpmm),1);
  73.       xminor=max(round(htmin*ypmm),1);
  74.       yminor=max(round(htmin*xpmm),1);
  75.  
  76.       xtick1=xtick;
  77.       nxsub1=nxsub;
  78.       ytick1=ytick;
  79.       nysub1=nysub;
  80.  
  81.       lax=strpos(xopt,'A')!=-1 || strpos(xopt,'a')!=-1;
  82.       lbx=strpos(xopt,'B')!=-1 || strpos(xopt,'b')!=-1;
  83.       lcx=strpos(xopt,'C')!=-1 || strpos(xopt,'c')!=-1;
  84.       lgx=strpos(xopt,'G')!=-1 || strpos(xopt,'g')!=-1;
  85.       lix=strpos(xopt,'I')!=-1 || strpos(xopt,'i')!=-1;
  86.       llx=strpos(xopt,'L')!=-1 || strpos(xopt,'l')!=-1;
  87.       lmx=strpos(xopt,'M')!=-1 || strpos(xopt,'m')!=-1;
  88.       lnx=strpos(xopt,'N')!=-1 || strpos(xopt,'n')!=-1;
  89.       lsx=strpos(xopt,'S')!=-1 || strpos(xopt,'s')!=-1;
  90.       ltx=strpos(xopt,'T')!=-1 || strpos(xopt,'t')!=-1;
  91.  
  92.       lay=strpos(yopt,'A')!=-1 || strpos(yopt,'a')!=-1;
  93.       lby=strpos(yopt,'B')!=-1 || strpos(yopt,'b')!=-1;
  94.       lcy=strpos(yopt,'C')!=-1 || strpos(yopt,'c')!=-1;
  95.       lgy=strpos(yopt,'G')!=-1 || strpos(yopt,'g')!=-1;
  96.       liy=strpos(yopt,'I')!=-1 || strpos(yopt,'i')!=-1;
  97.       lly=strpos(yopt,'L')!=-1 || strpos(yopt,'l')!=-1;
  98.       lmy=strpos(yopt,'M')!=-1 || strpos(yopt,'m')!=-1;
  99.       lny=strpos(yopt,'N')!=-1 || strpos(yopt,'n')!=-1;
  100.       lsy=strpos(yopt,'S')!=-1 || strpos(yopt,'s')!=-1;
  101.       lty=strpos(yopt,'T')!=-1 || strpos(yopt,'t')!=-1;
  102.       lvy=strpos(yopt,'V')!=-1 || strpos(yopt,'v')!=-1;
  103.  
  104.       gvpw(&vpwxmi,&vpwxma,&vpwymi,&vpwyma);
  105.       lax=lax && (vpwymi*vpwyma<0.0) && !llx;
  106.       lay=lay && (vpwxmi*vpwxma<0.0) && !lly;
  107.       if (llx) xtick1=1.0;
  108.       if (lly) ytick1=1.0;
  109.       if (ltx || lgx) 
  110.           pldtik(vpwxmi,vpwxma,&xtick1,&nxsub1,&xmode,&xprec);
  111.       if (lty || lgy) 
  112.           pldtik(vpwymi,vpwyma,&ytick1,&nysub1,&ymode,&yprec);
  113.  
  114. /* Set up tick variables */
  115.  
  116.       if (lix) {
  117.         i1x=xminor;
  118.         i2x=0;
  119.         i3x=xmajor;
  120.         i4x=0;
  121.       }
  122.       else {
  123.         i1x=0;
  124.         i2x=xminor;
  125.         i3x=0;
  126.         i4x=xmajor;
  127.       }
  128.  
  129.       if (liy) {
  130.         i1y=yminor;
  131.         i2y=0;
  132.         i3y=ymajor;
  133.         i4y=0;
  134.       }
  135.       else {
  136.         i1y=0;
  137.         i2y=yminor;
  138.         i3y=0;
  139.         i4y=ymajor;
  140.       }
  141.  
  142. /* Draw the bottom edge of the box */
  143.  
  144.       if (lbx) {
  145.         movphy(vppxmi,vppymi);
  146.         if (ltx) {
  147.           tp=xtick1*floor(vpwxmi/xtick1);
  148. bedge:
  149.           tn=tp+xtick1;
  150.           if (lsx) {
  151.             if (llx) {
  152.               for(i=0; i<=7;i++){
  153.                 temp=tp+xlog[i];
  154.                 if (betw(temp,vpwxmi,vpwxma))
  155.                      plxtik(wcpcx(temp),vppymi,i1x,i2x);
  156.               }
  157.             }
  158.             else {
  159.               for ( i=1;i<=nxsub1-1;i++) {
  160.                 temp=tp+i*(tn-tp)/nxsub1;
  161.                 if (betw(temp,vpwxmi,vpwxma))
  162.                      plxtik(wcpcx(temp),vppymi,i1x,i2x);
  163.               }
  164.             }
  165.           }
  166.           temp=tn;
  167.           if (betw(temp,vpwxmi,vpwxma)) {
  168.             plxtik(wcpcx(temp),vppymi,i3x,i4x);
  169.             tp=tn;
  170.             goto bedge;
  171.           }
  172.         }
  173.         draphy(vppxma,vppymi);
  174.       }
  175.                                            
  176. /* Draw right-hand edge of box */
  177.  
  178.       if (lcy) {
  179.         movphy(vppxma,vppymi);
  180.         if (lty) {
  181.           tp=ytick1*floor(vpwymi/ytick1);
  182. redge:
  183.           tn=tp+ytick1;
  184.           if (lsy) {
  185.             if (lly) {
  186.               for(i=0;i<=7;i++) {
  187.                 temp=tp+xlog[i];
  188.                 if (betw(temp,vpwymi,vpwyma))
  189.                      plytik(vppxma,wcpcy(temp),i2y,i1y);
  190.               }
  191.             }
  192.             else {
  193.               for(i=1;i<=nysub1-1;i++) {
  194.                 temp=tp+i*(tn-tp)/nysub1;
  195.                 if (betw(temp,vpwymi,vpwyma))
  196.                      plytik(vppxma,wcpcy(temp),i2y,i1y);
  197.               }
  198.             }
  199.           }
  200.           temp=tn;
  201.           if (betw(temp,vpwymi,vpwyma)) {
  202.             plytik(vppxma,wcpcy(temp),i4y,i3y);
  203.             tp=tn;
  204.             goto redge;
  205.           }
  206.         }
  207.         draphy(vppxma,vppyma);
  208.       }
  209.  
  210. /* Draw the top edge of the box */
  211.  
  212.       if (lcx) {
  213.         movphy(vppxma,vppyma);
  214.         if (ltx) {
  215.           tp=xtick1*(floor(vpwxma/xtick1)+1);
  216. tedge:
  217.           tn=tp-xtick1;
  218.           if (lsx) {
  219.             if (llx) {
  220.               for(i=7;i>=0;i--) {
  221.                 temp=tn+xlog[i];
  222.                 if (betw(temp,vpwxmi,vpwxma))
  223.                      plxtik(wcpcx(temp),vppyma,i2x,i1x);
  224.               }
  225.             }
  226.             else {
  227.               for(i=nxsub1-1;i>=1;i--) {
  228.                 temp=tn+i*(tp-tn)/nxsub1;
  229.                 if (betw(temp,vpwxmi,vpwxma))
  230.                      plxtik(wcpcx(temp),vppyma,i2x,i1x);
  231.               }
  232.             }
  233.           }
  234.           temp=tn;
  235.           if (betw(temp,vpwxmi,vpwxma)) {
  236.             plxtik(wcpcx(temp),vppyma,i4x,i3x);
  237.             tp=tn;
  238.             goto tedge;
  239.           }
  240.         }
  241.         draphy(vppxmi,vppyma);
  242.       }
  243.  
  244. /* Draw left-hand edge of box */
  245.  
  246.       if (lby) {
  247.         movphy(vppxmi,vppyma);
  248.         if (lty) {
  249.           tp=ytick1*(floor(vpwyma/ytick1)+1);
  250. ledge:
  251.           tn=tp-ytick1;
  252.           if (lsy) {
  253.             if (lly) {
  254.               for(i=7;i>=0;i--) {
  255.                 temp=tn+xlog[i];
  256.                 if (betw(temp,vpwymi,vpwyma))
  257.                      plytik(vppxmi,wcpcy(temp),i1y,i2y);
  258.               }
  259.             }
  260.             else {
  261.               for(i=nysub1-1;i>=1;i--) {
  262.                 temp=tn+i*(tp-tn)/nysub1;
  263.                 if (betw(temp,vpwymi,vpwyma))
  264.                      plytik(vppxmi,wcpcy(temp),i1y,i2y);
  265.               }
  266.             }
  267.           }
  268.           temp=tn;
  269.           if (betw(temp,vpwymi,vpwyma)) {
  270.             plytik(vppxmi,wcpcy(temp),i3y,i4y);
  271.             tp=tn;
  272.             goto ledge;
  273.           }
  274.         }
  275.         draphy(vppxmi,vppymi);
  276.       }
  277.  
  278. /* Draw the horizontal axis */
  279.  
  280.       if (lax) {
  281.         it0=wcpcy(0.0);
  282.         movphy(vppxmi,it0);
  283.         if (ltx) {
  284.           tp=xtick1*floor(vpwxmi/xtick1);
  285. haxis:
  286.           tn=tp+xtick1;
  287.           if (lsx) {
  288.             if (llx) {
  289.               for(i=0;i<=7;i++) {
  290.                 temp=tp+xlog[i];
  291.                 if (betw(temp,vpwxmi,vpwxma))
  292.                      plxtik(wcpcx(temp),it0,xminor,xminor);
  293.               }
  294.             }
  295.             else {
  296.               for(i=1;i<=nxsub1-1;i++) {
  297.                 temp=tp+i*(tn-tp)/nxsub1;
  298.                 if (betw(temp,vpwxmi,vpwxma))
  299.                      plxtik(wcpcx(temp),it0,xminor,xminor);
  300.               }
  301.             }
  302.           }
  303.           temp=tn;
  304.           if (betw(temp,vpwxmi,vpwxma)) {
  305.             plxtik(wcpcx(temp),it0,xmajor,xmajor);
  306.             tp=tn;
  307.             goto haxis;
  308.           }
  309.         }
  310.         draphy(vppxma,it0);
  311.       }
  312.  
  313. /* Draw the vertical axis */
  314.  
  315.       if (lay) {
  316.         it0=wcpcx(0.0);
  317.         movphy(it0,vppymi);
  318.         if (lty) {
  319.           tp=ytick1*floor(vpwymi/ytick1);
  320. vaxis:
  321.           tn=tp+ytick1;
  322.           if (lsy) {
  323.             if (lly) {
  324.               for(i=0;i<=7;i++) {
  325.                 temp=tp+xlog[i];
  326.                 if (betw(temp,vpwymi,vpwyma))
  327.                    plytik(it0,wcpcy(temp),yminor,yminor);
  328.               }
  329.             }
  330.             else {
  331.               for(i=1;i<=nysub1-1;i++) {
  332.                 temp=tp+i*(tn-tp)/nysub1;
  333.                 if (betw(temp,vpwymi,vpwyma))
  334.                      plytik(it0,wcpcy(temp),yminor,yminor);
  335.               }
  336.             }
  337.           }
  338.           temp=tn;
  339.           if (betw(temp,vpwymi,vpwyma)) {
  340.             plytik(it0,wcpcy(temp),ymajor,ymajor);
  341.             tp=tn;
  342.             goto vaxis;
  343.           }
  344.         }
  345.         draphy(it0,vppyma);
  346.       }
  347.  
  348. /* Draw grid in x direction */
  349.  
  350.       if (lgx) {
  351.         tp=xtick1*floor(vpwxmi/xtick1);
  352. xgrid:
  353.           tn=tp+xtick1;
  354.           if (betw(tn,vpwxmi,vpwxma)) {
  355.             pljoin(tn,vpwymi,tn,vpwyma);
  356.             tp=tn;
  357.             goto xgrid;
  358.           }
  359.       }
  360.  
  361. /* Draw grid in y direction */
  362.  
  363.       if (lgy) {
  364.         tp=ytick1*floor(vpwymi/ytick1);
  365. ygrid:
  366.           tn=tp+ytick1;
  367.           if (betw(tn,vpwymi,vpwyma)) {
  368.             pljoin(vpwxmi,tn,vpwxma,tn);
  369.             tp=tn;
  370.             goto ygrid;
  371.           }
  372.       }
  373.  
  374. /* Write horizontal label(s) */
  375.  
  376.       if ((lmx || lnx) && ltx) {
  377.         tp=xtick1*floor(vpwxmi/xtick1);
  378. hlabel:
  379.         tn=tp+xtick1;
  380.         if (betw(tn,vpwxmi,vpwxma)) {
  381.           if (!llx)
  382.             plform(tn,xmode,xprec,string);
  383.           else {
  384.             sprintf(strtmp,"%-d",round(tn));
  385.             strcpy(string,"10\\u");
  386.             strcat(string,strtmp);
  387.           }
  388.           pos=(tn-vpwxmi)/(vpwxma-vpwxmi);
  389.           if (lnx) plmtex("b",1.5,pos,0.5,string);
  390.           if (lmx) plmtex("t",1.5,pos,0.5,string);
  391.           tp=tn;
  392.           goto hlabel;
  393.         }
  394.       }
  395.  
  396. /* Write vertical label(s) */
  397.  
  398.       if ((lmy || lny) && lty) {
  399.         tp=ytick1*floor(vpwymi/ytick1);
  400. vlabel:
  401.         tn=tp+ytick1             ;
  402.         if (betw(tn,vpwymi,vpwyma)) {
  403.           if (!lly) 
  404.             plform(tn,ymode,yprec,string);
  405.           else {
  406.             sprintf(strtmp,"%-d",round(tn));
  407.             strcpy(string,"10\\u");
  408.             strcat(string,strtmp);
  409.           }
  410.           pos=(tn-vpwymi)/(vpwyma-vpwymi);
  411.           if (lny) {
  412.             if (lvy)
  413.               plmtex("lv",0.5,pos,1.0,string);
  414.             else
  415.               plmtex("l",1.5,pos,0.5,string);
  416.           }
  417.           if (lmy) {
  418.             if (lvy) 
  419.               plmtex("rv",0.5,pos,0.0,string);
  420.             else
  421.               plmtex("r",1.5,pos,0.5,string);
  422.           }
  423.           tp=tn;
  424.           goto vlabel;
  425.         }
  426.       }
  427.  
  428. /* Restore the clip limits to viewport edge */
  429.  
  430.       sclp(lxmin,lxmax,lymin,lymax);
  431. }
  432.