-
Notifications
You must be signed in to change notification settings - Fork 0
/
histgram.c
249 lines (193 loc) · 5.46 KB
/
histgram.c
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/*
* histgram.c
* Patater GUI Kit
*
* Created by Jaeden Amero on 2021-04-15.
* Copyright 2021. SPDX-License-Identifier: AGPL-3.0-or-later
*/
#include "guikit/histgram.h"
#include "guikit/graphics.h"
#include "guikit/prandom.h"
#include "guikit/primrect.h"
#include "guikit/panic.h"
#include <stdio.h>
static void drawAxes(const struct Rect *r)
{
int width;
int height;
width = r->right - r->left + 2; /* + 2 to include the vertical axis */
height = r->bottom - r->top + 1;
/* y-axis */
SetColor(COLOR_BLUE);
DrawVertLine(r->left - 1, r->top, height);
/* x-axis */
SetColor(COLOR_GREEN);
DrawHorizLine(r->left - 1, r->bottom + 1, width);
}
void RandomHistograms(int num)
{
int i;
for (i = 0; i < num; ++i)
{
struct Histogram *h;
struct Rect rect;
int color;
int numBuckets;
int maxValue;
int maxSize;
int valuesToAdd;
int j;
int width;
int fattness;
color = RandRange(0, NUM_COLORS - 1);
rect.left = RandRange(-10, SCREEN_WIDTH + 9);
rect.top = RandRange(-10, SCREEN_HEIGHT + 9);
rect.right = RandRange(0, SCREEN_WIDTH + 9);
rect.bottom = RandRange(0, SCREEN_HEIGHT + 9);
NormalizedRect(&rect, &rect);
width = rect.right - rect.left;
numBuckets = width - 1;
/* Width zero histograms aren't interesting to see. Also, our
* "pretty" histogram generator doesn't handle generating an
* appropriate number of buckets when the width is 0. */
numBuckets = numBuckets > 0 ? numBuckets : 1;
/* Make some bars fat. */
/* To be pretty, the number of buckets should be an integer dividend
* of the rect width */
fattness = RandRange(0, 10);
for (j = 0; j < fattness; ++j)
{
numBuckets /= 2;
}
numBuckets = numBuckets > 0 ? numBuckets : 1;
maxValue = RandRange(1, 1000000);
maxSize = numBuckets * 2;
h = HistogramAlloc(numBuckets);
HistogramSetMaxValue(h, maxValue);
valuesToAdd = RandRange(100, maxSize * 2);
#if 0
printf("numBuckets %d\n", numBuckets);
printf("maxValue %d\n", maxValue);
printf("maxSize %d\n", maxSize / numBuckets);
printf("valuesToAdd %d\n", valuesToAdd);
printf("width %d\n", width);
#endif
for (j = 0; j < valuesToAdd; ++j)
{
int val;
val = RandRange(0, maxValue);
HistogramPush(h, val);
}
SetColor(COLOR_WHITE);
FillScreen();
/*FillRect(COLOR_WHITE, &rect);*/
drawAxes(&rect);
DrawHistogram(h, color, &rect);
HistogramFree(h);
ShowGraphics();
}
}
void RandomHistogram(int samples)
{
struct Histogram *h;
struct Rect rect;
int numBuckets;
int maxValue;
int j;
int width;
rect.left = 20;
rect.top = 20;
rect.right = SCREEN_WIDTH - 1 - 20;
rect.bottom = SCREEN_HEIGHT - 1 - 20;
width = rect.right - rect.left + 1;
numBuckets = width;
maxValue = 2147483647;
h = HistogramAlloc(numBuckets);
HistogramSetMaxValue(h, maxValue);
for (j = 0; j < samples; ++j)
{
int val;
val = RandRange(0, maxValue);
/*val = prandom() & 0x7FFFFFFF; *//* Cap to int max */
HistogramPush(h, val);
}
SetColor(COLOR_WHITE);
FillScreen();
/*FillRect(COLOR_WHITE, &rect);*/
/* XXX Looks height is not obeyed... Should we clip? or let histogram
* outgrow its rect? Let it be ugly if the client asks for it? */
drawAxes(&rect);
DrawHistogram(h, COLOR_RED, &rect);
HistogramFree(h);
ShowGraphics();
}
void SampledHistogram(int samples)
{
struct Histogram *u;
struct Histogram *n;
struct Rect rect;
int i;
int width;
int numBuckets;
int maxValue;
int minSize;
int maxSize;
rect.left = 20;
rect.top = 20;
rect.right = SCREEN_WIDTH - 1 - 20;
rect.bottom = SCREEN_HEIGHT - 1 - 20;
width = rect.right - rect.left + 1;
numBuckets = width;
maxValue = 0x7FFFFFFF;
u = HistogramAlloc(numBuckets);
HistogramSetMaxValue(u, maxValue);
HistogramSetMinValue(u, 0);
for (i = 0; i < samples; ++i)
{
int val;
val = prandom() & 0x7FFFFFFF; /* Cap to int max */
HistogramPush(u, val);
}
/* Monte Carlo! Samping the samples to show sampling theorem works. */
minSize = HistogramGetMinSize(u);
maxSize = HistogramGetMaxSize(u);
n = HistogramAlloc(maxSize - minSize);
HistogramSetMaxValue(n, maxSize);
HistogramSetMinValue(n, minSize);
for (i = 0; i < samples; ++i)
{
size_t which;
int value;
which = RandRange(0, numBuckets - 1);
value = HistogramGet(u, which);
HistogramPush(n, value);
}
SetColor(COLOR_WHITE);
FillScreen();
/*FillRect(COLOR_WHITE, &rect);*/
drawAxes(&rect);
DrawHistogram(n, COLOR_RED, &rect);
HistogramFree(u);
HistogramFree(n);
ShowGraphics();
}
int main(void)
{
int ret;
ret = InitGraphics();
if (ret < 0)
{
panic("Couldn't init graphics\n");
}
printf("Drawing 10000 histograms...\n");
SetColor(COLOR_WHITE);
FillScreen();
ShowGraphics();
/*RandomHistograms(10000);*/
/*RandomHistogram(1000000);*/
SampledHistogram(10000);
SaveScreenShot("histgram.bmp");
FreeGraphics();
printf("Goodbye.\n");
return 0;
}