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