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