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

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