GHC new build system megapatch
[ghc-hetmet.git] / utils / hp2ps / AreaBelow.c
1 #include "Main.h"
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include "Defines.h"
5 #include "Error.h"
6 #include "HpFile.h"
7 #include "Utilities.h"
8
9 /* own stuff */
10 #include "AreaBelow.h"
11
12 /*
13  *      Return the area enclosed by all of the curves. The algorithm
14  *      used is the same as the trapizoidal rule for integration. 
15  */
16  
17 floatish
18 AreaBelow()
19 {
20     intish i;
21     intish j;
22     intish bucket;
23     floatish value;
24     struct chunk *ch;
25     floatish area;
26     floatish trap;
27     floatish base;
28     floatish *maxima;
29
30     maxima = (floatish *) xmalloc(nsamples * sizeof(floatish));
31     for (i = 0; i < nsamples; i++) {
32         maxima[i] = 0.0;
33     }   
34
35     for (i = 0; i < nidents; i++) {
36         for (ch = identtable[i]->chk; ch; ch = ch->next) {
37             for (j = 0; j < ch->nd; j++) {
38                 bucket = ch->d[j].bucket;
39                 value  = ch->d[j].value;
40                 if (bucket >= nsamples)
41                     Disaster("bucket out of range");
42                 maxima[ bucket ] += value;
43             }   
44         }    
45     }    
46
47     area = 0.0;
48
49     for (i = 1; i < nsamples; i++) {
50         base = samplemap[i] - samplemap[i-1];
51         if (maxima[i] > maxima[i-1]) {
52             trap = base * maxima[i-1] + ((base * (maxima[i] - maxima[i-1]))/ 2.0);
53         } else {
54             trap = base * maxima[i]   + ((base * (maxima[i-1] - maxima[i]))/ 2.0);
55         }
56
57         area += trap;
58     }
59
60     free(maxima);
61     return area;
62 }