home *** CD-ROM | disk | FTP | other *** search
/ Fish 'n' More 2 / fishmore-publicdomainlibraryvol.ii1991xetec.iso / disks / disk433.lzh / Gwin / exsrc.lzh / graph.c < prev    next >
C/C++ Source or Header  |  1991-01-12  |  3KB  |  174 lines

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