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