home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 344b.lha / plplot_v2.6 / src / plhist.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-01-27  |  1.5 KB  |  60 lines

  1. /* Draws a histogram of n values of a variable in array data[0..n-1] */
  2. /* in the range datmin to datmax using nbin bins. If "oldwin" is   */
  3. /* 1, the histogram is plotted in the current window. If not,   */
  4. /* the routine calls "plenv" to set up the graphics environment.   */
  5.  
  6. #include "plplot.h"
  7. #include <math.h>
  8. #ifdef PLSTDC
  9. #include <stdlib.h>
  10. #else
  11. extern char *malloc();
  12. extern void free();
  13. #endif
  14.  
  15. void plhist(n,data,datmin,datmax,nbin,oldwin)
  16. PLINT n, nbin, oldwin;
  17. PLFLT *data, datmin, datmax;
  18. {
  19.       PLINT bin, level;
  20.       PLFLT *x, *y, dx, ymax;
  21.       short i;
  22.  
  23.       glev(&level);
  24.       if (level<1) plexit("Please call plstar before plhist.");
  25.       if (level<3 && oldwin)
  26.               plexit("Please set up window before calling plhist.");
  27.       if (datmin >= datmax)
  28.               plexit("Data range invalid in plhist.");
  29.  
  30.       if(!(x = (PLFLT *)malloc(nbin*sizeof(PLFLT))))
  31.          plexit("Out of memory in plhist");
  32.       if(!(y = (PLFLT *)malloc(nbin*sizeof(PLFLT))))
  33.          plexit("Out of memory in plhist");
  34.  
  35.       dx = (datmax-datmin) / nbin;
  36.       for (i=0; i<nbin; i++) {
  37.         x[i] = datmin + i*dx;
  38.         y[i] = 0.0;
  39.       }
  40.  
  41.       for (i=0; i<n; i++)  {
  42.         bin = (data[i] - datmin)/dx;
  43.         bin = bin > 0 ? bin : 0;
  44.         bin = bin < nbin ? bin : nbin-1;
  45.         y[bin]++;
  46.       }
  47.  
  48.       if (!oldwin) {
  49.         ymax = 0.0;
  50.         for (i=0; i<nbin; i++) ymax = max(ymax,y[i]);
  51.         plenv(datmin,datmax,0.0,1.1*ymax,0,0);
  52.       }
  53.  
  54.       plbin(nbin,x,y,0);
  55.       free((VOID *)x);
  56.       free((VOID *)y);
  57. }
  58.  
  59.  
  60.