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