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