Mercurial > public > algo-animator
comparison main.c @ 10:f4e0c266321a
bubble sort
author | Dennis C. M. <dennis@denniscm.com> |
---|---|
date | Fri, 23 Jun 2023 20:24:37 +0100 |
parents | b7da0083b706 |
children | aaecc9b7ca9c |
comparison
equal
deleted
inserted
replaced
9:b7da0083b706 | 10:f4e0c266321a |
---|---|
1 #include <stdio.h> | 1 #include <stdio.h> |
2 #include <stdlib.h> | 2 #include <stdlib.h> |
3 #include <GL/glut.h> | 3 #include <GL/glut.h> |
4 #include <math.h> | 4 #include <math.h> |
5 #include <time.h> | |
5 | 6 |
6 | 7 |
7 #define WINDOW_HEIGHT 1080 | 8 #define WINDOW_HEIGHT 1080 |
8 #define WINDOW_WIDTH 1920 | 9 #define WINDOW_WIDTH 1920 |
10 #define OFFSET 150 | |
9 #define RECT_WIDTH 5 | 11 #define RECT_WIDTH 5 |
10 #define SPACE 5 | 12 #define SPACE 1 |
11 | 13 |
12 | 14 |
13 // Globals | 15 // Globals |
14 char* algos[] = {"Bubble sort", "Selection sort", "Insertion sort", "Quick sort"}; | 16 char* algos[] = {"Bubble sort", "Selection sort", "Insertion sort", "Quick sort"}; |
15 | |
16 int selected_algo = 0; | 17 int selected_algo = 0; |
17 int n_algos = sizeof(algos) / sizeof(algos[0]); | 18 int n_algos = sizeof(algos) / sizeof(algos[0]); |
18 int n_rectangles; | 19 |
19 | 20 int* arr; |
20 struct Rectangle { | 21 int arr_size; |
21 int width; | |
22 int height; | |
23 int x; | |
24 }; | |
25 | |
26 struct Rectangle* rectangles; | |
27 | 22 |
28 | 23 |
29 // Algos | 24 // Algos |
30 void bubble_sort() { | 25 void bubble_sort() { |
31 for (int i = 0; i < n_rectangles; i++) { | 26 for (int step = 0; step < arr_size - 1; step++) { |
32 | 27 |
33 struct Rectangle current = rectangles[i]; | 28 for (int i = 0; i < arr_size - step - 1; i++) { |
34 struct Rectangle next = rectangles[i + 1]; | 29 int current = arr[i]; |
35 | 30 int next = arr[i + 1]; |
36 printf("Current pos: %i\n", current.x); | 31 |
37 printf("Next pos: %i\n", next.x); | 32 if (current > next) { |
38 | 33 arr[i + 1] = current; |
39 // Place largest element at the end of the array | 34 arr[i] = next; |
40 if (current.height > next.height) { | 35 } |
41 rectangles[i + 1].x = current.x; | |
42 rectangles[i].x = next.x; | |
43 } | 36 } |
44 } | 37 } |
45 } | 38 } |
46 | 39 |
47 | 40 |
48 // Utils | 41 void create_array() { |
49 int random_int(int min, int max) { | 42 arr_size = floor((WINDOW_WIDTH - RECT_WIDTH) / (RECT_WIDTH + SPACE)) + 1; |
50 return rand() % ((max - min) + 1) + min; | 43 arr = (int*)malloc(arr_size * sizeof(int)); |
44 | |
45 // srand(time(NULL)); | |
46 | |
47 int min = OFFSET; | |
48 int max = WINDOW_HEIGHT - OFFSET; | |
49 | |
50 for (int i = 0; i < arr_size; i++) { | |
51 arr[i] = rand() % ((max - min) + 1) + min; | |
52 } | |
53 } | |
54 | |
55 | |
56 void print_array() { | |
57 for (int i = 0; i < arr_size; i++) { | |
58 printf("%d ", arr[i]); | |
59 } | |
51 } | 60 } |
52 | 61 |
53 | 62 |
54 void algo_selector(int direction) { | 63 void algo_selector(int direction) { |
55 int selection = selected_algo + direction; | 64 int selection = selected_algo + direction; |
73 | 82 |
74 // Matrix projection and reset with identity | 83 // Matrix projection and reset with identity |
75 glMatrixMode(GL_PROJECTION); | 84 glMatrixMode(GL_PROJECTION); |
76 glLoadIdentity(); | 85 glLoadIdentity(); |
77 | 86 |
78 // Set the coordinates to be used with the viewport | 87 /* |
79 gluOrtho2D(0, WINDOW_WIDTH, WINDOW_HEIGHT, 0); | 88 * Creates projection matrix |
89 * x increases from left to right (0 to WINDOW_WIDTH) | |
90 * y increases from bottom to top (0 to WINDOW_HEIGHT) | |
91 */ | |
92 gluOrtho2D(0, WINDOW_WIDTH, 0, WINDOW_HEIGHT); | |
80 | 93 |
81 } | 94 } |
82 | 95 |
83 | 96 |
84 void render_text(int x, int y, char* text) { | 97 void render_text(int x, int y, char* text) { |
92 | 105 |
93 void display() { | 106 void display() { |
94 glClear(GL_COLOR_BUFFER_BIT); | 107 glClear(GL_COLOR_BUFFER_BIT); |
95 glBegin(GL_QUADS); | 108 glBegin(GL_QUADS); |
96 | 109 |
97 for (int i = 0; i < n_rectangles; i++) { | 110 int x = 0; |
98 glVertex2f(rectangles[i].x, WINDOW_HEIGHT - 200); | 111 for (int i = 0; i < arr_size; i++) { |
99 glVertex2f(rectangles[i].x + rectangles[i].width, WINDOW_HEIGHT - 200); | 112 |
100 glVertex2f(rectangles[i].x + rectangles[i].width, rectangles[i].height); | 113 // Bottom left |
101 glVertex2d(rectangles[i].x, rectangles[i].height); | 114 glVertex2f(x, OFFSET); |
115 | |
116 // Top left | |
117 glVertex2f(x, arr[i]); | |
118 | |
119 // Top right | |
120 glVertex2f(x + RECT_WIDTH, arr[i]); | |
121 | |
122 // Bottom right | |
123 glVertex2f(x + RECT_WIDTH, OFFSET); | |
124 | |
125 x += RECT_WIDTH + SPACE; | |
102 } | 126 } |
103 | 127 |
104 glEnd(); | 128 glEnd(); |
105 | 129 |
106 // Render text | 130 // Render text |
107 char text[256]; | 131 char text[256]; |
108 | 132 |
109 sprintf(text, "Algorithm: %s", algos[selected_algo]); | 133 sprintf(text, "Algorithm: %s", algos[selected_algo]); |
110 render_text(20.0, WINDOW_HEIGHT - 130, text); | 134 render_text(20, OFFSET - 50, text); |
111 | 135 |
112 sprintf(text, "Number of elements: %i", n_rectangles); | 136 sprintf(text, "Number of elements: %i", arr_size); |
113 render_text(20.0, WINDOW_HEIGHT - 100, text); | 137 render_text(20, OFFSET - 75, text); |
114 | 138 |
115 render_text(WINDOW_WIDTH - 500, WINDOW_HEIGHT - 130, "Press 'a' or 's' to select an algorithm"); | 139 render_text(WINDOW_WIDTH - 500, OFFSET - 50, "Press 'a' or 's' to select an algorithm"); |
116 render_text(WINDOW_WIDTH - 500, WINDOW_HEIGHT - 100, "Press 'enter' to run the algorithm"); | 140 render_text(WINDOW_WIDTH - 500, OFFSET - 75, "Press 'enter' to run the algorithm"); |
141 render_text(WINDOW_WIDTH - 500, OFFSET - 100, "Press 'r' to reset array"); | |
117 | 142 |
118 glutSwapBuffers(); | 143 glutSwapBuffers(); |
119 glFlush(); | 144 glFlush(); |
120 } | 145 } |
121 | 146 |
125 } | 150 } |
126 | 151 |
127 | 152 |
128 void keyboard(unsigned char key, int x, int y) { | 153 void keyboard(unsigned char key, int x, int y) { |
129 | 154 |
130 // S | 155 // s |
131 if (key == 115) { | 156 if (key == 115) { |
132 algo_selector(1); | 157 algo_selector(1); |
133 } | 158 } |
134 | 159 |
135 // A | 160 // r |
161 if (key == 114) { | |
162 create_array(); | |
163 } | |
164 | |
165 // a | |
136 if (key == 97) { | 166 if (key == 97) { |
137 algo_selector(-1); | 167 algo_selector(-1); |
138 } | 168 } |
139 | 169 |
140 // Enter | 170 // enter |
141 if (key == 13) { | 171 if (key == 13) { |
172 printf("Before sorting: "); | |
173 print_array(); | |
174 printf("\n\n"); | |
175 | |
142 bubble_sort(); | 176 bubble_sort(); |
177 | |
178 printf("After sorting: "); | |
179 print_array(); | |
143 } | 180 } |
144 } | 181 } |
145 | 182 |
146 | 183 |
147 int main(int argc, char** argv) { | 184 int main(int argc, char** argv) { |
148 n_rectangles = floor((WINDOW_WIDTH - RECT_WIDTH) / (RECT_WIDTH + SPACE)) + 1; | 185 create_array(); |
149 rectangles = malloc(n_rectangles * sizeof(struct Rectangle)); | |
150 | |
151 int x_pos = 1; | |
152 | |
153 int i = 0; | |
154 while (i < n_rectangles) { | |
155 rectangles[i].width = RECT_WIDTH; | |
156 rectangles[i].height = random_int(100, WINDOW_HEIGHT - 200); | |
157 rectangles[i].x = x_pos; | |
158 | |
159 x_pos += RECT_WIDTH + SPACE; | |
160 i++; | |
161 } | |
162 | 186 |
163 glutInit(&argc, argv); | 187 glutInit(&argc, argv); |
164 glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB); | 188 glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB); |
165 glutInitWindowSize(WINDOW_WIDTH, WINDOW_HEIGHT); | 189 glutInitWindowSize(WINDOW_WIDTH, WINDOW_HEIGHT); |
166 glutCreateWindow("OpenGL Window"); | 190 glutCreateWindow("OpenGL Window"); |
168 glutDisplayFunc(display); | 192 glutDisplayFunc(display); |
169 glutKeyboardFunc(keyboard); | 193 glutKeyboardFunc(keyboard); |
170 glutIdleFunc(idle); | 194 glutIdleFunc(idle); |
171 glutMainLoop(); | 195 glutMainLoop(); |
172 | 196 |
173 free(rectangles); | 197 free(arr); |
174 | 198 |
175 return 0; | 199 return 0; |
176 } | 200 } |
177 | 201 |
178 | 202 |