source: branches/fitshead-branch/Utils/cpgcumul.c @ 1441

Last change on this file since 1441 was 86, checked in by Matthew Whiting, 18 years ago

Large commit, mostly removing dud commented code, and adding comments and
documentation to the existing code. No new features.

File size: 1.7 KB
Line 
1#include <stdio.h>
2#include <stdlib.h>
3#include <cpgplot.h>
4#include <math.h>
5void _swap(float &a, float &b);
6void _sort(float *array, int begin, int end);
7void cpgcumul(int npts, float *data, float datamin, float datamax, int pgflag)
8{
9  /**
10   *  cpgcumul(npts, data, datamin, datamax, pgflag)
11   *    A new pgplot routine that draws a cumulative distribution.
12   *   The use of pgflag is similar to cpghist & cpghist_log:
13   *    0 --> draw a new graph using cpgenv, going from 0 to 1 on the y-axis.
14   *    2 --> draw the plot on the current graph, without re-drawing any axes.
15   */
16
17  int i;
18  float *sorted;
19  float MINCOUNT=0.;
20
21  sorted = (float *)calloc(npts,sizeof(float));
22  for(i=0;i<npts;i++) sorted[i] = data[i];
23  _sort(sorted,0,npts);
24
25  cpgbbuf();
26
27  // DEFINE ENVIRONMENT IF NECESSARY
28  if(pgflag == 0) cpgenv(datamin,datamax,MINCOUNT,1.0001,0,0);
29  if(pgflag == 2) cpgswin(datamin,datamax,MINCOUNT,1.);
30
31  // DRAW LINE
32  cpgmove(datamin,MINCOUNT);
33  for(i=0;i<npts;i++) cpgdraw(sorted[i],(float)(i+1)/(float)(npts));
34  cpgdraw(datamax,1.);
35
36  cpgebuf();
37 
38  free(sorted);
39
40}
41
42
43void _swap(float &a, float &b)
44{
45  float t=a; a=b; b=t;
46}
47
48void _sort(float *array, int begin, int end)
49{
50  float pivot = array[begin];
51  int i = begin + 1, j = end, k = end;
52  float t;
53
54  while (i < j) {
55    if (array[i] < pivot) i++;
56    else if (array[i] > pivot) {
57      j--; k--;
58      t = array[i];
59      array[i] = array[j];
60      array[j] = array[k];
61      array[k] = t; }
62    else {
63      j--;
64      _swap(array[i], array[j]);
65    }  }
66  i--;
67  _swap(array[begin], array[i]);       
68  if (i - begin > 1)
69    _sort(array, begin, i);
70  if (end - k   > 1)
71    _sort(array, k, end);                     
72}
Note: See TracBrowser for help on using the repository browser.