home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / alib / d5xx / d571 / gwin.lha / Gwin / Examples / graph3.c < prev    next >
C/C++ Source or Header  |  1991-12-22  |  3KB  |  188 lines

  1. /* ROUTINE TO GRAPH REAL POINTS FROM STANDARD INPUT */
  2. #include "gwin.user.h"
  3.  
  4. float datax[5000];
  5. float datay[5000];
  6. char label[100][100];
  7. float xmin,xmax;
  8. int xstart,xstop;
  9. float xdelta;
  10. float green = 2.0;
  11. float blue = 3.0;
  12. float white = 7.0;
  13. float black = 0.0;
  14. float red = 1.0;
  15. int quitfunction();
  16. int barchart();
  17. int barplot = 0;
  18. int graphchart();
  19. int graphplot = 1;
  20. float ymin,ymax;
  21. float xx,xtic;
  22. float xwin1,xwin2,ywin1,ywin2;
  23. char chstr[100];
  24. int nn;
  25.  
  26. main(argc,argv)
  27. int argc;
  28. char *argv[];
  29. {
  30. int i,j;
  31. char line[256];
  32.  
  33.    printf("\n\nIf you are typing in datax, datay via standard input\n");
  34.    printf("enter carriage return then CTRL-\\ to end datax input.\n\n");
  35.  
  36.    xstart=0;
  37.  
  38.    i=0;
  39.    while( gets(line) > 0){
  40.       for(j=0;j<strlen(line);j++){
  41.          if(line[j] == ',') line[j] = ' ';
  42.       }
  43.       sscanf(line,"%f %f %s",&datax[i],&datay[i],label[i]);
  44.       i++;
  45.    }
  46.    nn = i;
  47.  
  48.    graph();
  49. }
  50. graph()
  51. {
  52. int i;
  53. float x,y;
  54. int makegrid;
  55.  
  56.    xmax = -1e10;
  57.    xmin =  1e10;
  58.  
  59.    for(i=0;i<nn;i++) {
  60.       if(datax[i]>xmax)xmax=datax[i];
  61.       if(datax[i]<xmin)xmin=datax[i];
  62.    }
  63.  
  64.  
  65.    ymax = -1e10;
  66.    ymin =  1e10;
  67.  
  68.    for(i=0;i<nn;i++) {
  69.       if(datay[i]>ymax)ymax=datay[i];
  70.       if(datay[i]<ymin)ymin=datay[i];
  71.    }
  72.  
  73.    ymax = ymax + .05*(ymax-ymin);
  74.    ymin = ymin - .05*(ymax-ymin);
  75.  
  76.  
  77.    USTART("high2",0.,640.,0.,400.);
  78.    uamenu(G,1,0,0,"FUNCTION",' ',0,MIDRAWN|MENUENABLED,0);
  79.    uamenu(G,1,1,0,"GRAPH   ",'G',0,MIDRAWN|ITEMTEXT|HIGHCOMP
  80.              |COMMSEQ|ITEMENABLED,graphchart);
  81.  
  82.    uamenu(G,1,2,0,"BARCHART",'B',0,MIDRAWN|ITEMTEXT|HIGHCOMP
  83.              |COMMSEQ|ITEMENABLED,barchart);
  84.  
  85.    uamenu(G,1,3,0,"QUIT    ",'Q',0,MIDRAWN|ITEMTEXT|HIGHCOMP
  86.              |COMMSEQ|ITEMENABLED,quitfunction);
  87.  
  88.    makegrid = TRUE;
  89.  
  90.    xwin1 =  xmin;
  91.    xwin2 =  xmax;
  92.    ywin1 =  ymin;
  93.    ywin2 =  ymax+.05*(ymax-ymin);
  94.  
  95.    uwindo(G,xwin1,xwin2,ywin1,ywin2);
  96.  
  97.    redraw();
  98.  
  99.    while(TRUE){
  100.       if(ugrinl(G,&x,&y,&uuev)){UEND();exit(0);};
  101.       upset(G,"colo",white);
  102.  
  103.       sprintf(chstr,"X = %10.3f",x);
  104.       uprint(G,( xwin1 + .1*(xwin2-xwin1) ),ymax,chstr);
  105.  
  106.       sprintf(chstr,"Y = %10.3f",y);
  107.       uprint(G,( xwin1 + .4*(xwin2-xwin1) ),ymax,chstr);
  108.    }
  109. }
  110.  
  111.  
  112. redraw()
  113. {
  114. int i,j;
  115. int color;
  116. float xinc;
  117. char tempstr[2];
  118.  
  119.    upset(G,"colo",black);
  120.    uset(G,"fill");
  121.    uoutln(G);
  122.    uset(G,"nofi");
  123.  
  124.    upset(G,"colo",red);
  125.  
  126.    umove(G,xmin, ymin);
  127.    udraw(G,xmax,ymin);
  128.    udraw(G,xmax,ymax);
  129.    udraw(G,xmin, ymax);
  130.    udraw(G,xmin, ymin);
  131.  
  132.    umove(G,datax[0],datay[0]);
  133.  
  134.  
  135.    if(graphplot){
  136.       /* DRAW GRAPH */
  137.       upset(G,"colo",green);
  138.       uwindo(G,xwin1,xwin2,ywin1,ywin2);
  139.       for(i=0;i<nn;i++) udraw(G,datax[i],datay[i]);
  140.    }
  141.  
  142.    if(barplot){
  143.       /* DRAW BAR CHART */
  144.       xinc = (xmax-xmin)/nn;
  145.       uset(G,"rint");
  146.       uwindo(G,xwin1,xwin2,ywin1,ywin2);
  147.       color = 0;
  148.       for (i=0;i<nn;i++){
  149.          if((color++ % 15) == 0) color = 1;
  150.          upset(G,"colo",(float)color);
  151.          uset(G,"fill");
  152.          urect(G,xinc*i,ymin,xinc*(i+1),datay[i]);
  153.          uset(G,"nofi");
  154.  
  155.          upset(G,"colo",white);
  156.          upset(G,"bcol",black);
  157.          for(j=0;j<strlen(label[i]);j++){
  158.             tempstr[0]=label[i][j];
  159.             tempstr[1] = '\0';
  160.             uprint(G,xinc*i,ymin+.5*(ymax-ymin)-.02*j*(ymax-ymin),tempstr);
  161.          }
  162.  
  163.       }
  164.    }
  165. }
  166.  
  167. graphchart()
  168. {
  169. barplot = 0;
  170. graphplot = 1;
  171. redraw();
  172. }
  173.  
  174. barchart()
  175. {
  176. barplot = 1;
  177. graphplot = 0;
  178. redraw();
  179. }
  180.  
  181. quitfunction()
  182. {
  183. UEND();
  184. exit();
  185. }
  186.  
  187.  
  188.