home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Simtel MSDOS 1996 September
/
Simtel-MSDOS-Sep1996-CD2.iso
/
disc2
/
c
/
scale.cq
/
SCALE.C
Wrap
Text File
|
1985-09-24
|
7KB
|
275 lines
/* scale - calculate scaling parameters for plotting so that axes
have convenient labels
*/
#include "math.h"
#include <g.h>
#include <g3.h>
/*
#define xxxx
#define xx
*/
#define NTIC 30.
#define NLAB 6
#define minimum(x,y) (((x)<(y))?(x):(y))
#define maximum(x,y) (((x)>(y))?(x):(y))
#define power(x,y) (exp(log(x)*(y)))
#ifdef xxxx
static int used[4][4]= { {0,0,0,0},
{0,0,0,0},
{0,0,0,0},
{0,0,0,0}
};
#endif
static double decide[4][3]= { {1.414, 3.162, 7.071 },
{1.414, 1.414, 1.414 },
{2.236, 2.236, 2.236 },
{1.414, 3.162, 7.071 }
};
static int mtic[4][4]= { {1, 2, 5, 10},
{1, 2, 2, 2},
{1, 5, 5, 5},
{1, 2, 5, 10}
};
static double x1; /* minimum x value */
static double x2; /* maximum x value */
static int kx; /* kind of axis...0 for linear, 1 for log */
static int nlx; /* number of long tic marks */
static int ntx; /* number of short tic marks */
static double y1, y2; /* (similar for y's) */
static int ky, nly, nty;
scale (amin,amax,ka,bmin,bmax,kb)
double amin,amax,bmin,bmax;
int ka,kb;
{
#ifdef xxx
printf("scale: amin=%f amax=%f ka=%d \n", amin, amax, ka);
#endif
scale_one(amin,amax,&x1,&x2,&nlx,&ntx,ka);
#ifdef xxx
printf("scale: x1=%f x2=%f nlx=%d ntx=%d \n",x1,x2,nlx,ntx);
printf("scale: bmin=%f bmax=%f kb=%d \n", bmin, bmax, kb);
#endif
scale_one(bmin,bmax,&y1,&y2,&nly,&nty,kb);
#ifdef xxx
printf("scale: y1=%f y2=%f nly=%d nty=%d \n",y1,y2,nly,nty);
#endif
kx=ka; ky=kb;
window(x1,x2,y1,y2);
}
scale_one (amin,amax,bmin,bmax,nlab,ntic,kind)
double amin,amax,*bmin,*bmax; int *nlab, *ntic, kind;
{ double tens, top, bottom, fraction, d, interval;
int i,j;
if(!kind)
{amax=(amax>amin) ? amax : amin+1.;
fraction=(amax-amin)/maximum(NLAB-1,1);
#ifdef xxx
printf("\nscale_one: fraction=%f",fraction);
#endif
tens=power(10.,floor(log10(fraction)));
fraction/=tens;
for (j=0; j<=2; j++) if(fraction<decide[0][j]) break;
d=mtic[0][j]*tens;
bottom=floor(amin/d); *bmin=bottom*d;
top=ceil(amax/d); *bmax=top*d;
*nlab=(int)(top-bottom+.25);
interval=NTIC/ *nlab;
for (i=0; i<=2; i++) if(interval<decide[j][i]) break;
*ntic=mtic[j][i]* *nlab;
#ifdef xxx
used[j][i]++;
printf("(-->%f) ?= d=%f, tens=%f \n",fraction,d,tens);
printf(" bottom=%f, top=%f, interval=%f \n",bottom,top,interval);
printf("i=%d j=%d bmin=%f bmax=%f \n", i, j, *bmin, *bmax);
#endif
}
else if (kind)
{*bmin=floor(amin); *bmax=ceil(amax);
*nlab= *bmax - *bmin + .1;
if ((*nlab * *nlab * 9) > 900) *ntic=*nlab;
else *ntic = *nlab*9;
}
#ifdef xx
printf("\n scale_one: *bmin=%f *bmax=%f *nlab=%d *ntic=%d kind=%d \n",
*bmin, *bmax, *nlab, *ntic, kind);
#endif
}
static double segl[9]={.301, .176, .125, .097, .079, .067, .058, .051, .046};
axis(numbers,grid_style) int numbers,grid_style;
{ int i,j,after;
double t1,t11,e1,t2,t22,e2,s,x,y,yval;
static double ch; /* height of a character (in world coordinates) */
static double cw; /* width of a character (in world coordinates) */
static double wx1,wx2,wy1,wy2; /* window limits */
static double vx1,vx2,vy1,vy2; /* viewport limits */
char buf[80], format[80];
if(grid_style==0) return;
inquire_window(&wx1,&wx2,&wy1,&wy2);
inquire_viewport_2(&vx1,&vx2,&vy1,&vy2);
cw=(wx2-wx1)/(vx2-vx1)*char_width/pixels_wide;
ch=(wy2-wy1)/(vy2-vy1)*char_height/pixels_high;
t1=(x2-x1)*.012; /* length of small tic mark */
e1=t1*.0000001; /* invisibly small distance */
t2=(y2-y1)*.015;
e2=t2*.0000001;
if(grid_style==1) {t11=t1*2.; t22=t2*2.;} /* length of long tic mark */
else {t11=x2-x1; t22=0.;}
y_axis(y1,y2,ky,nly,nty,x1+e1,t1,t11); /* left */
x_axis(x1,x2,kx,nlx,ntx,y2-e2,-t2,-t22); /* top */
if(grid_style==2) {t11=0.; t22=y2-y1;}
x_axis(x1,x2,kx,nlx,ntx,y1+e2,t2,t22); /* bottom */
y_axis(y1,y2,ky,nly,nty,x2-e1,-t1,-t11); /* right */
if(numbers)
{clip_window(0);
if(!ky) /* "after" is # digits needed after decimal */
{after=ceil(-log10((y2-y1)/nly));
if(after<0) after=0;
sprintf(format,"%%%d.%df",after,after);
}
x=x1; y=y2-ch; yval=y2;
for(i=0; i<=nly; i++) /* label y axis */
{if(!ky) sprintf(buf,format,yval); else sprintf(buf,"1e%1.0f",yval);
move_abs_2(x-cw*(1.+strlen(buf)),y);
text(buf);
y-=(y2-ch-y1)/nly; yval-=(y2-y1)/nly;
}
if(!kx)
{after=ceil(-log10((x2-x1)/nlx));
if(after<0) after=0;
sprintf(format,"%%%d.%df",after,after);
}
x=x1; y=y1-1.5*ch;
for(i=0; i<=nlx; i++) /* label x axis */
{if(!kx) sprintf(buf,format,x); else sprintf(buf,"1e%1.0f",x);
move_abs_2(x-cw*strlen(buf),y);
text(buf);
x+=(x2-x1)/nlx;
}
clip_window(1);
}
}
static x_axis(x,x2,kx,nlx,ntx,y,t,tlarge)
double x, x2, y, t, tlarge;
int kx, nlx, ntx;
{ int i, j; double s;
if(!kx)
{s=(x2-x)/ntx;
for( i=nlx ; i ; i-- )
{for ( j=ntx/nlx ; j ; j-- )
{move_abs_2(x,y);
line_abs_2(x+=s,y);
line_abs_2(x,y+t);
}
line_abs_2(x,y+tlarge);
}
}
else if(kx)
{s=(x2-x)/nlx;
for ( i=nlx ; i ; i-- )
{if(ntx>nlx)
{for ( j=0 ; j<9 ; j++ )
{move_abs_2(x,y);
line_abs_2(x+=s*segl[j],y);
line_abs_2(x,y+t);
}
line_abs_2(x,y+tlarge);
}
else
{move_abs_2(x,y);
line_abs_2(x+=s,y);
line_abs_2(x,y+tlarge);
}
}
}
}
static y_axis(y,y2,ky,nly,nty,x,t,tlarge)
double y, y2, x, t, tlarge;
int ky, nly, nty;
{ int i,j; double s;
if(!ky)
{s=(y2-y)/nty;
for( i=nly ; i ; i-- )
{for ( j=nty/nly ; j ; j-- )
{move_abs_2(x,y);
line_abs_2(x,y+=s);
line_abs_2(x+t,y);
}
line_abs_2(x+tlarge,y);
}
}
else if(ky)
{s=(y2-y)/nly;
for ( i=nly ; i ; i-- )
{if(nty>nly)
{for ( j=0 ; j<9 ; j++ )
{move_abs_2(x,y);
line_abs_2(x,y+=s*segl[j]);
line_abs_2(x+t,y);
}
line_abs_2(x+tlarge,y);
}
else
{move_abs_2(x,y);
line_abs_2(x,y+=s);
line_abs_2(x+tlarge,y);
}
}
}
}
#ifdef xxxx
main()
{ double lower, upper, delt, span;
int nlab, ntic, i, j;
initialize_core(1);
initialize_view_surface(1);
ndc_space_2(1.,.8);
/* viewport2(.1,1.,.1,.8); */
clip_window(1);
delt=power(10.,0.1);
lower=-5.;
for (i=4; i; i--)
{span=1.;
for (j=11; j; j--)
{new_frame();
/* generate the figure */
upper=lower+span;
scale(lower,upper,2,lower,upper,2);
create_temporary_segment();
axis();
printf("\n lower=%10.4f...upper=%10.4f \n",
lower,upper);
close_temporary_segment();
span*=delt;
getchar();
}
lower+=3.;
}
printf("mtic[][]...\n");
for(j=0; j<4; j++)
{for(i=0; i<4; i++) printf("%4d",mtic[j][i]);
printf("\n");
}
printf("usage of cells of mtic[][]...\n");
for(j=0; j<4; j++)
{for(i=0; i<4; i++) printf("%4d",used[j][i]);
printf("\n");
}
}
#endif