home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS 1996 September / Simtel-MSDOS-Sep1996-CD2.iso / disc2 / c / scale.cq / SCALE.C
Text File  |  1985-09-24  |  7KB  |  275 lines

  1. /*    scale - calculate scaling parameters for plotting so that axes
  2.             have convenient labels
  3. */
  4.  
  5. #include "math.h"
  6. #include <g.h>
  7. #include <g3.h>
  8.  
  9. /*
  10. #define xxxx
  11. #define xx
  12. */
  13.  
  14. #define NTIC 30.
  15. #define NLAB 6
  16. #define minimum(x,y) (((x)<(y))?(x):(y))
  17. #define maximum(x,y) (((x)>(y))?(x):(y))
  18. #define power(x,y) (exp(log(x)*(y)))
  19.  
  20. #ifdef xxxx
  21.  
  22. static int used[4][4]=        {    {0,0,0,0},
  23.                                 {0,0,0,0},
  24.                                 {0,0,0,0},
  25.                                 {0,0,0,0}
  26.                             };
  27. #endif
  28.  
  29. static double decide[4][3]=    {    {1.414,    3.162,    7.071    },
  30.                                 {1.414,    1.414,    1.414    },
  31.                                 {2.236,    2.236,    2.236    },
  32.                                 {1.414,    3.162,    7.071    }
  33.                             };
  34. static int mtic[4][4]=        {    {1,  2,  5, 10},
  35.                                 {1,  2,  2,  2},
  36.                                 {1,  5,  5,  5},
  37.                                 {1,  2,  5, 10}
  38.                             };
  39. static double    x1;        /* minimum x value */
  40. static double    x2;        /* maximum x value */
  41. static int        kx;        /* kind of axis...0 for linear, 1 for log */
  42. static int        nlx;    /* number of long tic marks */
  43. static int        ntx;    /* number of short tic marks */
  44. static double    y1, y2;    /* (similar for y's) */
  45. static int        ky, nly, nty;
  46.  
  47. scale (amin,amax,ka,bmin,bmax,kb)
  48.     double amin,amax,bmin,bmax;
  49.     int ka,kb;
  50. {
  51. #ifdef xxx
  52.     printf("scale: amin=%f  amax=%f  ka=%d \n", amin, amax, ka);
  53. #endif
  54.     scale_one(amin,amax,&x1,&x2,&nlx,&ntx,ka);
  55. #ifdef xxx
  56.     printf("scale: x1=%f x2=%f nlx=%d ntx=%d \n",x1,x2,nlx,ntx);
  57.     printf("scale: bmin=%f  bmax=%f  kb=%d \n", bmin, bmax, kb);
  58. #endif
  59.     scale_one(bmin,bmax,&y1,&y2,&nly,&nty,kb);
  60. #ifdef xxx
  61.     printf("scale: y1=%f y2=%f nly=%d nty=%d \n",y1,y2,nly,nty);
  62. #endif
  63.     kx=ka; ky=kb;
  64.     window(x1,x2,y1,y2);
  65. }
  66.  
  67. scale_one (amin,amax,bmin,bmax,nlab,ntic,kind)
  68.     double amin,amax,*bmin,*bmax; int *nlab, *ntic, kind;
  69. {    double tens, top, bottom, fraction, d, interval;
  70.     int i,j;
  71.  
  72.     if(!kind)
  73.         {amax=(amax>amin) ? amax : amin+1.;
  74.         fraction=(amax-amin)/maximum(NLAB-1,1);
  75. #ifdef xxx
  76.         printf("\nscale_one: fraction=%f",fraction);
  77. #endif
  78.         tens=power(10.,floor(log10(fraction)));
  79.         fraction/=tens;
  80.         for (j=0; j<=2; j++) if(fraction<decide[0][j]) break;
  81.         d=mtic[0][j]*tens;
  82.         bottom=floor(amin/d);    *bmin=bottom*d;
  83.         top=ceil(amax/d);        *bmax=top*d;
  84.         *nlab=(int)(top-bottom+.25);
  85.         interval=NTIC/ *nlab;
  86.         for (i=0; i<=2; i++) if(interval<decide[j][i]) break;
  87.         *ntic=mtic[j][i]* *nlab;
  88. #ifdef xxx
  89.         used[j][i]++;
  90.         printf("(-->%f) ?= d=%f, tens=%f \n",fraction,d,tens);
  91.         printf("       bottom=%f, top=%f, interval=%f \n",bottom,top,interval);
  92.         printf("i=%d  j=%d  bmin=%f  bmax=%f  \n", i, j, *bmin, *bmax);
  93. #endif
  94.         }
  95.     else if (kind)
  96.         {*bmin=floor(amin); *bmax=ceil(amax);
  97.         *nlab= *bmax - *bmin + .1;
  98.         if ((*nlab * *nlab * 9) > 900) *ntic=*nlab;
  99.         else *ntic = *nlab*9;
  100.         }
  101. #ifdef xx
  102.     printf("\n scale_one: *bmin=%f  *bmax=%f  *nlab=%d  *ntic=%d  kind=%d \n",
  103.         *bmin, *bmax, *nlab, *ntic, kind);
  104. #endif
  105. }
  106.  
  107. static double segl[9]={.301, .176, .125, .097, .079, .067, .058, .051, .046};
  108.  
  109. axis(numbers,grid_style) int numbers,grid_style;
  110. {    int i,j,after;
  111.     double t1,t11,e1,t2,t22,e2,s,x,y,yval;
  112.     static double ch;        /* height of a character (in world coordinates) */
  113.     static double cw;        /* width of a character (in world coordinates) */
  114.     static double wx1,wx2,wy1,wy2;    /* window limits */
  115.     static double vx1,vx2,vy1,vy2;    /* viewport limits */
  116.     char buf[80], format[80];
  117.     if(grid_style==0) return;
  118.     inquire_window(&wx1,&wx2,&wy1,&wy2);
  119.     inquire_viewport_2(&vx1,&vx2,&vy1,&vy2);
  120.     cw=(wx2-wx1)/(vx2-vx1)*char_width/pixels_wide;
  121.     ch=(wy2-wy1)/(vy2-vy1)*char_height/pixels_high;
  122.     t1=(x2-x1)*.012;    /* length of small tic mark */
  123.     e1=t1*.0000001;        /* invisibly small distance */
  124.     t2=(y2-y1)*.015;
  125.     e2=t2*.0000001;
  126.     if(grid_style==1) {t11=t1*2.; t22=t2*2.;}    /* length of long tic mark */
  127.     else {t11=x2-x1; t22=0.;}
  128.     y_axis(y1,y2,ky,nly,nty,x1+e1,t1,t11);    /* left        */
  129.     x_axis(x1,x2,kx,nlx,ntx,y2-e2,-t2,-t22);    /* top        */
  130.     if(grid_style==2) {t11=0.; t22=y2-y1;}
  131.     x_axis(x1,x2,kx,nlx,ntx,y1+e2,t2,t22);    /* bottom    */
  132.     y_axis(y1,y2,ky,nly,nty,x2-e1,-t1,-t11);    /* right    */
  133.     if(numbers)
  134.         {clip_window(0);
  135.         if(!ky)                    /* "after" is # digits needed after decimal */
  136.             {after=ceil(-log10((y2-y1)/nly));
  137.             if(after<0) after=0;
  138.             sprintf(format,"%%%d.%df",after,after);
  139.             }
  140.         x=x1; y=y2-ch; yval=y2;
  141.         for(i=0; i<=nly; i++)        /* label y axis */
  142.             {if(!ky) sprintf(buf,format,yval); else sprintf(buf,"1e%1.0f",yval);
  143.             move_abs_2(x-cw*(1.+strlen(buf)),y);
  144.             text(buf);
  145.             y-=(y2-ch-y1)/nly; yval-=(y2-y1)/nly;
  146.             }
  147.         if(!kx)
  148.             {after=ceil(-log10((x2-x1)/nlx));
  149.             if(after<0) after=0;
  150.             sprintf(format,"%%%d.%df",after,after);
  151.             }
  152.         x=x1; y=y1-1.5*ch;
  153.         for(i=0; i<=nlx; i++)        /* label x axis */
  154.             {if(!kx) sprintf(buf,format,x); else sprintf(buf,"1e%1.0f",x);
  155.             move_abs_2(x-cw*strlen(buf),y);
  156.             text(buf);
  157.             x+=(x2-x1)/nlx;
  158.             }
  159.         clip_window(1);
  160.         }
  161. }
  162.  
  163. static x_axis(x,x2,kx,nlx,ntx,y,t,tlarge)
  164. double x, x2, y, t, tlarge;
  165. int kx, nlx, ntx;
  166. {    int i, j; double s;
  167.     if(!kx)
  168.         {s=(x2-x)/ntx;
  169.         for( i=nlx ; i ; i-- )
  170.             {for ( j=ntx/nlx ; j ; j-- )
  171.                 {move_abs_2(x,y);
  172.                 line_abs_2(x+=s,y);
  173.                 line_abs_2(x,y+t);
  174.                 }
  175.             line_abs_2(x,y+tlarge);
  176.             }
  177.         }
  178.     else if(kx)
  179.         {s=(x2-x)/nlx;
  180.         for ( i=nlx ; i ; i-- )
  181.             {if(ntx>nlx)
  182.                 {for ( j=0 ; j<9 ; j++ )
  183.                     {move_abs_2(x,y);
  184.                     line_abs_2(x+=s*segl[j],y);
  185.                     line_abs_2(x,y+t);
  186.                     }
  187.                 line_abs_2(x,y+tlarge);
  188.                 }
  189.             else
  190.                 {move_abs_2(x,y);
  191.                 line_abs_2(x+=s,y);
  192.                 line_abs_2(x,y+tlarge);
  193.                 }
  194.             }
  195.         }
  196. }
  197.  
  198. static y_axis(y,y2,ky,nly,nty,x,t,tlarge)
  199. double y, y2, x, t, tlarge;
  200. int ky, nly, nty;
  201. {    int i,j; double s;
  202.     if(!ky)
  203.         {s=(y2-y)/nty;
  204.         for( i=nly ; i ; i-- )
  205.             {for ( j=nty/nly ; j ; j-- )
  206.                 {move_abs_2(x,y);
  207.                 line_abs_2(x,y+=s);
  208.                 line_abs_2(x+t,y);
  209.                 }
  210.             line_abs_2(x+tlarge,y);
  211.             }
  212.         }
  213.     else if(ky)
  214.         {s=(y2-y)/nly;
  215.         for ( i=nly ; i ; i-- )
  216.             {if(nty>nly)
  217.                 {for ( j=0 ; j<9 ; j++ )
  218.                     {move_abs_2(x,y);
  219.                     line_abs_2(x,y+=s*segl[j]);
  220.                     line_abs_2(x+t,y);
  221.                     }
  222.                 line_abs_2(x+tlarge,y);
  223.                 }
  224.             else
  225.                 {move_abs_2(x,y);
  226.                 line_abs_2(x,y+=s);
  227.                 line_abs_2(x+tlarge,y);
  228.                 }
  229.             }
  230.         }
  231. }
  232.  
  233. #ifdef xxxx
  234. main()
  235. {    double lower, upper, delt, span;
  236.     int nlab, ntic, i, j;
  237.  
  238.     initialize_core(1);
  239.     initialize_view_surface(1);
  240.     ndc_space_2(1.,.8);
  241. /*    viewport2(.1,1.,.1,.8); */
  242.     clip_window(1);
  243.     delt=power(10.,0.1);
  244.     lower=-5.;
  245.     for (i=4; i; i--)
  246.         {span=1.;
  247.         for (j=11; j; j--)
  248.             {new_frame();
  249.             /* generate the figure */
  250.             upper=lower+span;
  251.             scale(lower,upper,2,lower,upper,2);
  252.             create_temporary_segment();
  253.             axis();
  254.             printf("\n lower=%10.4f...upper=%10.4f \n",
  255.                 lower,upper);
  256.             close_temporary_segment();    
  257.             span*=delt;
  258.             getchar();
  259.             }
  260.         lower+=3.;
  261.         }
  262.     printf("mtic[][]...\n");
  263.     for(j=0; j<4; j++)
  264.         {for(i=0; i<4; i++) printf("%4d",mtic[j][i]);
  265.         printf("\n");
  266.         }
  267.     printf("usage of cells of mtic[][]...\n");
  268.     for(j=0; j<4; j++)
  269.         {for(i=0; i<4; i++) printf("%4d",used[j][i]);
  270.         printf("\n");
  271.         }
  272. }
  273.  
  274. #endif
  275.