-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
146 lines (112 loc) · 3.85 KB
/
main.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
// Compile with
// cc main.c -lraylib -lGL -lm -lpthread -ldl -lrt -lX11
// link the library if necessary:
// // cc main.c -lraylib -L [PATH] -lGL -lm -lpthread -ldl -lrt -lX11
#include "raylib.h"
#include <stdlib.h>
const int screenWidth = 800;
const int screenHeight = 700;
// https://benpfaff.org/writings/clc/shuffle.html
void shuffle(int *v, int n) {
for (int i = 0; i < n - 1; ++i) {
int j = i + rand() / (RAND_MAX / (n - i) + 1);
int t = v[j];
v[j] = v[i];
v[i] = t;
}
}
void inicializa(int *v, int n) {
for (int i = 0; i < n; ++i)
v[i] = rand() % screenHeight;
}
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
void sceneMenu(void) {
ClearBackground(BLACK);
const int screenWidth = 800;
const int screenHeight = 700;
const char *menuTitle = "Visualizador de Algoritmos de Ordenacao";
DrawText(menuTitle, (screenWidth - MeasureText(menuTitle, 30)) / 2, screenHeight / 2 - 150, 30, WHITE);
const char *menuSubtitle = "Trabalho final da disciplina de Algoritmos E Estruturas De Dados I 2024";
DrawText(menuSubtitle, (screenWidth - MeasureText(menuSubtitle, 15)) / 2, screenHeight / 2 - 100, 15, WHITE);
const char *menuSubSubTitle = "Feito por Lucas Machado Cogrossi";
DrawText(menuSubSubTitle, (screenWidth - MeasureText(menuSubSubTitle, 15)) / 2, screenHeight / 2 - 75, 15, WHITE);
const char *menuOp1 = "Pressione ENTER para iniciar";
DrawText(menuOp1, (screenWidth - MeasureText(menuOp1, 20)) / 2, screenHeight / 2 + 100, 20, WHITE);
}
void sceneSubMenu(void) {
ClearBackground(BLACK);
const char *subMenuTitle = "Selecione o algoritmo";
DrawText(subMenuTitle, (screenWidth - MeasureText(subMenuTitle, 30)) / 2, 200, 30, WHITE);
const char *subMenuOp1 = "Pressione 1 para bubble sort";
DrawText(subMenuOp1, (screenWidth - MeasureText(subMenuOp1, 30)) / 2, 300, 30, WHITE);
const char *subMenuOp2 = "Mais algoritmos em breve...";
DrawText(subMenuOp2, (screenWidth - MeasureText(subMenuOp2, 30)) / 2, 400, 30, WHITE);
}
int main(void) {
int i = 0, j = 0;
bool startBubbleSort = false;
bool showMenu = true;
bool showSubMenu = false;
bool showVector = false;
bool startQuickSort = false;
InitWindow(screenWidth, screenHeight, "Trabalho final AED1");
SetTargetFPS(5000);
int n = screenWidth;
int *v;
v = (int*) malloc(n * sizeof(int));
inicializa(v, n);
shuffle(v, n);
while (!WindowShouldClose()) {
if (IsKeyPressed(KEY_ENTER) || IsKeyPressed(KEY_BACKSPACE)) {
showMenu = false;
showSubMenu = true;
shuffle(v, n);
showVector = false;
sceneSubMenu();
startBubbleSort = false;
i = 0;
j = 0;
}
if(showMenu) {
sceneMenu();
}
if(showSubMenu) {
sceneSubMenu();
}
if (IsKeyPressed(KEY_ONE)) {
showMenu = false;
showSubMenu = false;
showVector = true;
startBubbleSort = true;
}
BeginDrawing();
ClearBackground(BLACK);
if (startBubbleSort) {
int a = v[j];
int b = v[j + 1];
if (a > b) {
swap(&v[j], &v[j + 1]);
}
if (i < n) {
j++;
if (j >= n - i - 1) {
j = 0;
i++;
}
}
}
if (showVector) {
for (int k = 0; k < n; k++) {
DrawRectangle(k, screenHeight - v[k], 1, v[k], WHITE);
}
}
EndDrawing();
}
free(v);
CloseWindow();
return 0;
}