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
|
#define _GNU_SOURCE
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <time.h>
#include <raylib.h>
#include "distribution.h"
int
main (void)
{
const int screen_padding = 10;
const int notation_height = 20;
int screen_width = 800;
int screen_height = 450;
SetConfigFlags (FLAG_MSAA_4X_HINT |
FLAG_WINDOW_RESIZABLE |
FLAG_VSYNC_HINT |
FLAG_WINDOW_UNDECORATED);
InitWindow (screen_width, screen_height, "graph");
SetTargetFPS (60);
srand (time (NULL));
struct distribution dist;
// distribution_init (&dist, 20, 20, 1, 10);
distribution_init (&dist, 11, 20, 1, 10);
int arr[dist.n - 1];
memset (arr, 0, sizeof (arr));
int iterrations = 10000;
for (int i = 0; i < iterrations; ++i)
arr[distribution_rand (&dist)] += 1;
while (!WindowShouldClose ()) {
BeginDrawing ();
ClearBackground (WHITE);
screen_width = GetScreenWidth ();
screen_height = GetScreenHeight ();
const int rect_width = (screen_width - screen_padding)/(dist.n - 1) - screen_padding;
const int rect_max_height = screen_height - 2*screen_padding - notation_height;
for (int i = 0; i < dist.n - 1; ++i) {
const int rect_height = rect_max_height*arr[i]/iterrations;
DrawRectangle ((i + 1)*screen_padding + i*rect_width,
screen_padding + rect_max_height - rect_height,
rect_width, rect_height, BLACK);
char *perc;
asprintf (&perc, "%2d%%", arr[i]*100/iterrations);
DrawText (perc, (i + 1)*screen_padding + i*rect_width + rect_width/4, screen_height - screen_padding - 3*notation_height/4, notation_height, BLACK);
}
EndDrawing ();
}
CloseWindow ();
return EXIT_SUCCESS;
}
|