GHC new build system megapatch
[ghc-hetmet.git] / utils / hp2ps / Curves.c
1 #include "Main.h"
2 #include <stdio.h>
3 #include <math.h>
4 #include "Defines.h"
5 #include "Dimensions.h"
6 #include "HpFile.h"
7 #include "Shade.h"
8 #include "Utilities.h"
9
10 /* own stuff */
11 #include "Curves.h"
12
13 static floatish *x;             /* x and y values  */
14 static floatish *y;
15
16 static floatish *py;            /* previous y values */
17
18 static void Curve PROTO((struct entry *));      /* forward */
19 static void ShadeCurve
20     PROTO((floatish *x, floatish *y, floatish *py, floatish shade));
21
22 void
23 Curves()
24 {
25     intish i;
26
27     for (i = 0; i < nidents; i++) {
28         Curve(identtable[i]);
29     }
30 }
31
32 /*
33  *      Draw a curve, and fill the area that is below it and above 
34  *      the previous curve.
35  */
36
37 static void
38 Curve(e)
39   struct entry* e;
40 {
41     struct chunk* ch;
42     int j;
43   
44     for (ch = e->chk; ch; ch = ch->next) {
45         for (j = 0; j < ch->nd; j++) {
46             y[ ch->d[j].bucket ] += ch->d[j].value;
47         }
48     }    
49
50     ShadeCurve(x, y, py, ShadeOf(e->name));
51 }
52
53
54 static void PlotCurveLeftToRight PROTO((floatish *, floatish *)); /* forward */
55 static void PlotCurveRightToLeft PROTO((floatish *, floatish *)); /* forward */
56
57 static void SaveCurve PROTO((floatish *, floatish *)); /* forward */
58
59 /*
60  *      Map virtual x coord to physical x coord 
61  */
62  
63 floatish
64 xpage(x)
65   floatish x;
66 {
67     return (x + graphx0); 
68 }
69
70
71
72 /*
73  *      Map virtual y coord to physical y coord 
74  */
75  
76 floatish
77 ypage(y)
78   floatish y;
79 {
80     return (y + graphy0); 
81 }
82
83
84 /*
85  *      Fill the region bounded by two splines, using the given 
86  *      shade.
87  */
88
89 static void
90 ShadeCurve(x, y, py, shade)
91   floatish *x; floatish *y; floatish *py; floatish shade;
92 {
93     fprintf(psfp, "%f %f moveto\n", xpage(x[0]), ypage(py[0]));
94     PlotCurveLeftToRight(x, py);
95
96     fprintf(psfp, "%f %f lineto\n", xpage(x[nsamples - 1]), 
97                                     ypage(y[nsamples - 1]));
98     PlotCurveRightToLeft(x, y);
99
100     fprintf(psfp, "closepath\n");
101
102     fprintf(psfp, "gsave\n");
103
104     SetPSColour(shade);
105     fprintf(psfp, "fill\n");
106
107     fprintf(psfp, "grestore\n");
108     fprintf(psfp, "stroke\n");
109
110     SaveCurve(y, py);
111 }
112
113 static void
114 PlotCurveLeftToRight(x,y)
115   floatish *x; floatish *y;
116 {
117     intish i;
118
119     for (i = 0; i < nsamples; i++) {
120         fprintf(psfp, "%f %f lineto\n", xpage(x[i]), ypage(y[i]));
121     }
122 }
123
124 static void
125 PlotCurveRightToLeft(x,y)
126   floatish *x; floatish *y;
127 {
128     intish i;
129
130     for (i = nsamples - 1; i >= 0; i-- ) {
131         fprintf(psfp, "%f %f lineto\n", xpage(x[i]), ypage(y[i]));
132     }
133 }
134
135 /*
136  *      Save the curve coordinates stored in y[] in py[].
137  */
138
139 static void
140 SaveCurve(y, py)
141   floatish *y; floatish* py;
142 {
143     intish i;
144
145     for (i = 0; i < nsamples; i++) {
146         py[i] = y[i];
147     }
148 }
149
150 extern floatish xrange;
151
152 void
153 CurvesInit()
154 {
155     intish i;
156
157     x  =  (floatish*) xmalloc(nsamples * sizeof(floatish));
158     y  =  (floatish*) xmalloc(nsamples * sizeof(floatish));
159     py =  (floatish*) xmalloc(nsamples * sizeof(floatish));
160
161     for (i = 0; i < nsamples; i++) {
162         x[i] = ((samplemap[i] - samplemap[0])/ xrange) * graphwidth;
163         y[i] = py[i] = 0.0; 
164     }
165 }