summaryrefslogtreecommitdiff
path: root/ghc/utils/hp2ps/Curves.c
blob: ec05c98336053e02a8b24301ad0a3533c175085f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#include "Main.h"
#include <stdio.h>
#include <math.h>
#include "Defines.h"
#include "Dimensions.h"
#include "HpFile.h"
#include "Shade.h"
#include "Utilities.h"

/* own stuff */
#include "Curves.h"

static floatish *x;		/* x and y values  */
static floatish *y;

static floatish *py;		/* previous y values */

static void Curve PROTO((struct entry *));	/* forward */
static void ShadeCurve
    PROTO((floatish *x, floatish *y, floatish *py, floatish shade));

void
Curves()
{
    intish i;

    for (i = 0; i < nidents; i++) {
        Curve(identtable[i]);
    }
}

/*
 *      Draw a curve, and fill the area that is below it and above 
 *	the previous curve.
 */

static void
Curve(e)
  struct entry* e;
{
    struct chunk* ch;
    int j;
  
    for (ch = e->chk; ch; ch = ch->next) {
        for (j = 0; j < ch->nd; j++) {
	    y[ ch->d[j].bucket ] += ch->d[j].value;
	}
    }    

    ShadeCurve(x, y, py, ShadeOf(e->name));
}


static void PlotCurveLeftToRight PROTO((floatish *, floatish *)); /* forward */
static void PlotCurveRightToLeft PROTO((floatish *, floatish *)); /* forward */

static void SaveCurve PROTO((floatish *, floatish *)); /* forward */

/*
 *	Map virtual x coord to physical x coord 
 */
 
floatish
xpage(x)
  floatish x;
{
    return (x + graphx0); 
}



/*
 *	Map virtual y coord to physical y coord 
 */
 
floatish
ypage(y)
  floatish y;
{
    return (y + graphy0); 
}


/*
 *	Fill the region bounded by two splines, using the given 
 *	shade.
 */

static void
ShadeCurve(x, y, py, shade)
  floatish *x; floatish *y; floatish *py; floatish shade;
{
    fprintf(psfp, "%f %f moveto\n", xpage(x[0]), ypage(py[0]));
    PlotCurveLeftToRight(x, py);

    fprintf(psfp, "%f %f lineto\n", xpage(x[nsamples - 1]), 
                                    ypage(y[nsamples - 1]));
    PlotCurveRightToLeft(x, y);

    fprintf(psfp, "closepath\n");

    fprintf(psfp, "gsave\n");

    SetPSColour(shade);
    fprintf(psfp, "fill\n");

    fprintf(psfp, "grestore\n");
    fprintf(psfp, "stroke\n");

    SaveCurve(y, py);
}

static void
PlotCurveLeftToRight(x,y)
  floatish *x; floatish *y;
{
    intish i;

    for (i = 0; i < nsamples; i++) {
        fprintf(psfp, "%f %f lineto\n", xpage(x[i]), ypage(y[i]));
    }
}

static void
PlotCurveRightToLeft(x,y)
  floatish *x; floatish *y;
{
    intish i;

    for (i = nsamples - 1; i >= 0; i-- ) {
        fprintf(psfp, "%f %f lineto\n", xpage(x[i]), ypage(y[i]));
    }
}

/*
 *	Save the curve coordinates stored in y[] in py[].
 */

static void
SaveCurve(y, py)
  floatish *y; floatish* py;
{
    intish i;

    for (i = 0; i < nsamples; i++) {
	py[i] = y[i];
    }
}

extern floatish xrange;

void
CurvesInit()
{
    intish i;

    x  =  (floatish*) xmalloc(nsamples * sizeof(floatish));
    y  =  (floatish*) xmalloc(nsamples * sizeof(floatish));
    py =  (floatish*) xmalloc(nsamples * sizeof(floatish));

    for (i = 0; i < nsamples; i++) {
        x[i] = ((samplemap[i] - samplemap[0])/ xrange) * graphwidth;
        y[i] = py[i] = 0.0; 
    }
}